Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinError 2: File not found with Subprocess.run [duplicate]

Version: Python 3.7.5

OS: Windows 10

I am trying to fix the following 'file not found' error. The python code is shown below, followed by the error produced in the command line.

Python:

    subprocess.run(['start', 'current_roster.xlsx'], check=True)

File Not Found Error:

 Traceback (most recent call last):
   File "__winmain__.py", line 569, in <module>
     update_roster()
   File "__winmain__.py", line 480, in update_roster
     subprocess.run(['start', 'current_roster.xlsx'], check=True)
   File "C:\Python37\lib\subprocess.py", line 488, in run
     with Popen(*popenargs, **kwargs) as process:
   File "C:\Python37\lib\subprocess.py", line 800, in __init__
     restore_signals, start_new_session)
   File "C:\Python37\lib\subprocess.py", line 1207, in _execute_child
     startupinfo)
 FileNotFoundError: [WinError 2] The system cannot find the file specified

I have code for the above script on OSX as well, which is functional, but uses the 'open' command in place of 'start'. It does not produce the above error.

Does subprocess.run() not by default look for the specified file in the current working directory? I also get the same error when attempting:

    cwd = os.path.abspath(sys.argv[0])
    cwd = os.path.dirname(cwd)
    filepath = os.path.join(cwd, 'current_roster.xlsx')
    subprocess.run(['start', filepath], check = True)
like image 810
Xonu Avatar asked Jul 13 '26 02:07

Xonu


1 Answers

The error isn't saying that the argument wasn't found. It’s saying that the start command wasn't found, because it's an internal command. You can only use it from cmd.exe.

You can fix this using the change suggested by @facehugger in a comment:

try subprocess.run(['start', 'current_roster.xlsx'], shell=True)

like image 71
Anonymous Avatar answered Jul 14 '26 15:07

Anonymous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!