【发布时间】:2016-09-01 10:57:01
【问题描述】:
我找了好久还是没搞明白... 这是我出错的部分代码。
import subprocess as sp
import os
cmd_args = []
cmd_args.append('start ')
cmd_args.append('/wait ')
cmd_args.append(os.path.join(dirpath,filename))
print(cmd_args)
child = sp.Popen(cmd_args)
命令提示符通过这个。
['start ', '/wait ', 'C:\\Users\\xxx\\Desktop\\directory\\myexecutable.EXE']
Traceback (most recent call last):
File "InstallALL.py", line 89, in <module>
child = sp.Popen(cmd_args)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo)
FileNotFoundError: [WinError 2]
看起来文件路径有 2 个反斜杠错误。
我知道我是否这样做
print(os.path.join(dirpath,filename))
它会返回
C:\Users\xxx\Desktop\directory\myexecutable.EXE
我确定文件在那里。 我该如何调试?
【问题讨论】:
-
需要第一个反斜杠来转义第二个反斜杠,所以双反斜杠不是问题
-
你也可以试试
shell=True吗? -
路径正确。双反斜杠不是问题,它们是字符串的
repr的产物。无论如何:您确定问题是最后一个参数而不是start?start是 command 还是内置的 shell? -
@Idos 请注意,仅添加
shell=True不会产生预期的结果。您必须使用单个字符串参数来表示命令行,而不是传递shell=True时的参数列表,即Popen('start /wait <the-path>', shell=True)。 -
非常感谢大家。 Bakuriu的回答奏效了。事实上,我应该将我的论点作为字符串而不是列表传递。
标签: python