python - WinError 2:未使用 Subprocess.run 找到文件
问题描述
版本:Python 3.7.5
操作系统: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 上也有上述脚本的代码,它是功能性的,但使用“打开”命令代替“开始”。它不会产生上述错误。
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)
解决方案
该错误并不是说未找到该论点。start
意思是找不到该命令,因为它是一个内部命令。您只能从cmd.exe
.
您可以使用@facehugger在评论中建议的更改来解决此问题:
尝试
subprocess.run(['start', 'current_roster.xlsx'], shell=True)
推荐阅读
- javascript - 使用 javascript 弹出 iframe src 域名
- ios - 我可以在不拥有实际设备的情况下存档 Apple TV 应用程序吗?
- javascript - 以准确的放置位置拖放
- paypal-rest-sdk - PayPal JS SDK 在自定义试用期内通过?
- firebase - Flutter Web 应用程序:无法写入 Firebase
- django - 在 Django 更新后,Django 插入中的重复键违规
- c# - EF6 代码首先是多对多,具有与其中一个实体上的集合相同类型的附加属性
- javascript - 如何删除对象上的所有键,这些键不存在于 JavaScript 的数组中?
- php - fopen 返回 false 但目录存在
- nunit - Specflow - 发现不明确的步骤时引发构建错误