Search code examples
pythonyoutube-dl

os.system doesn't work until getting input in python


I am trying to run a youtube-dl command but when os.system(command) run, it is waiting an input on terminal(there is no input required). I press enter 2-3 time than it starts downloading.

url = sys.argv[1]
command = "youtube-dl " + url
print("downloading...")
os.system(command)
print("test")

I can't see "test" output. command works on cmd properly. waiting or subprocess command not working.

dont start properly


Solution

  • Using subprocess instead of os.system will let you set stdin=subprocess.DEVNULL so it can't read from stdin (without going to the TTY, which most programs don't). Also, passing a list and keeping the default shell=False avoids security issues where contents inside the URL could be treated by the shell as commands to run.

    subprocess.run(["youtube-dl", url], stdin=subprocess.DEVNULL)