. Advertisement .
..3..
. Advertisement .
..4..
Please help me solve the filenotfounderror: [winerror 2] the system cannot find the file specified problem.
Here is my entire error message:
>>> import subprocess
>>> subprocess.run(["dir", "/p"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 488, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Users\zhaosong\anaconda3\envs\env_python_37\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Hope to receive an answer soon about the cause of the error and how to handle it. I really appreciate everyone’s help. Thank.
The cause: This filenotfounderror: [winerror 2] the system cannot find the file specified error happens when you execute the aforementioned command in a window shell without passing the
shell=True
argument to the run method of thesubprocess
module.Solution: If you provide the
shell=True
argument in therun
method of thesubprocess
module, it will execute successfully as seen below: