Search code examples
pythonsubprocesspopen

Python - subprocess.popen - play mp4 using it's path


What is the problem I am facing- I am trying to run an mp4 from a file path using subprocess.popen.

Details - I originally tried: os.system - it works but it doesn't take a variable of a string. Or I am not converting my variable into a string correctly. I will show example pictures below.

subprocess.call - I couldn't get this to run an mp4 from vlc (I have to use call as I have to use python 2.7.18. I know its old but I am stuck on that for the program I am trying to use this with) subprocess.popen - I was able to get subprocess.popen to open up vlc player, but I wasn't able to make it run an mp4 using vlc. I am not sure if this is an argument issue or I am just not show how to write this correctly.

Main goal - I am trying to make a script where it asks me what I would like to search on my computer, and after I input what I would like to search it find the first thing it sees, then runs it. I am using os.walk to find the first file in my search, and I can print it, but I can't execute from the path value it finds.

My current code:

import os
import subprocess
def find(name):
    for root, dirs, files in os.walk('C:/Users/RKerrigan/Videos/w\\', followlinks=True):
        for file in files:
            if name in file:
                vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                music=str(os.path.join(root,file))
                print(music)
                subprocess.popen([vlc])
                return
    print("Finish")
try:
    s=raw_input("name: ")
    find(s)
except Exception as e:
    print(e)
    print("Error")

Pictures of the issue -

Me trying subprocess.popen in a very simple script to see if I can run a mp4 from a file path

Me trying subprocess.popen in a very simple script but using a variable for the path of the file. VLC will open but no mp4 will open. Only the base program.

Me trying subprocess.popen in the full script I am working on. I am using os.walk to find the first file it can, then I am trying to print it and run the actual file it finds. However Only VLC opens, no file.

Me trying os.system in the full script I am working on. I like os.system as it works when I put in a string to the file path. It will run an mp4 no problem but when I try to get the value of what I search using os.walk and use that as the path of the file it doesn't execute. Furthermore it doesn't give an errors like subprocess.popen does.

Me trying os.system but trying the str function to turn a value into a string for use. - It still didn't work for me or I am doing it wrong.


Solution

  • Have a close look at this part:

                    vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                    music=str(os.path.join(root,file))
                    print(music)
                    subprocess.popen([vlc])
    

    During each iteration of the loops, you've assign the same string to the vlc variable, 'C:/Program Files/VideoLAN/VLC/vlc.exe', making the subprocess.popen([vlc]) call do the exact same thing each time.

    The logical thing to do is use the discovered path, music, inside the popen() call:

                    vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                    music=str(os.path.join(root,file))
                    print(music)
                    subprocess.popen([vlc, music])