WinError 2:在Subprocess.run中找不到文件

问题描述 投票:1回答:1

版本:Python 3.7.5

OS:Windows 10

我正在尝试修复以下“找不到文件”错误。 python代码如下所示,其后是在命令行中产生的错误。

Python:

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

找不到文件错误:

 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

我在OSX上也具有上述脚本的代码,该代码可以正常运行,但是使用'open'命令代替'start'。它不会产生上述错误。

subprocess.run()默认情况下是否不在当前工作目录中查找指定的文件?尝试时也会收到相同的错误:

    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)
python windows subprocess
1个回答
0
投票

错误不是说未找到参数。就是说找不到start命令,因为它是internal command。您只能从cmd.exe使用它。

© www.soinside.com 2019 - 2024. All rights reserved.