I am currently creating an application in python which uses the Youtube Data API and includes the following code block:
import os
from subprocess import run
os.chdir(os.path.dirname(__file__))
command = 'python3.10 uploadYoutube.py --file="blank.mp4" --title="Blank" --description="THIS IS YOUR BLANK VIDEO" --keywords="blank" --category="20" --privacyStatus="private"'
terminal_output = run(command, capture_output=True).stdout
print(terminal_output)
This produces the error:
terminal_output = run(command, capture_output=True).stdout
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 503, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 971, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1456, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
Does anybody know how I could fix this error?
as Adon Bilivit stated, this is due to the first argument you pass to the command subprocess.run
.
As stated in the subprocess documentation, when you provide a string there is a platform dependent interpretation. On POSIX, providing a string
is interpreted as the name or path of the program to execute.
The first solution is to pass a sequence to the first subprocess.run
argument
subprocess.run(command.split(), capture_output=True)
You can also, as the documentation suggests, set shell
to True
.
If shell is True, it is recommended to pass args as a string rather than as a sequence.
subprocess.run(command, capture_output=True, shell=True)