Python Forum

Full Version: FileNotFoundError when using subprocess
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from subprocess import check_output
output=check_output(["ls", "F:\myData\input"]).decode("utf8")
print(ouptut)
I'm trying to run this code to view the files in this directory and save the results as output but this line is throwing an error upon execution.

Can anyone help me solve this and understand the issue? Sad

Error:
Traceback (most recent call last): File "F:\myData\input\Analysis.py", line 21, in output=check_output(["ls", "F:\myData\input\Analysis.py]).decode("utf8") File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 336, in check_output **kwargs).stdout File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in init restore_signals, start_new_session) File "C:\Users\Abhinav\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified
I set up f:\myData\input
and tried:
>>>from subprocess import check_output
>>>import sys
>>>try:
...    output=check_output(["ls", "F:\myData\input"]).decode("utf8")
... except subprocess.CalledProcessError:
...     print("Unexpected error:", sys.exc_info()[0])
...
>>>
without error.
check for typo, and make sure directory exists