“系统找不到指定的文件” 在 python 中调用 subprocess.Popen 时
我正在尝试使用 svnmerge.py 来合并一些文件。 它在底层使用 python,当我使用它时,我收到一个错误 - “系统找不到指定的文件”。 工作中的同事正在运行相同版本的 svnmerge.py 和 python(2.5.2,特别是 r252:60911),没有出现任何问题。
我找到了 此链接,它描述了我的问题。 尝试那里概述的内容,我确认 Python 可以找到 SVN(它在我的路径中):
P:\>python Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> i,k = os.popen4("svn --version") >>> i.close() >>> k.readline() 'svn, version 1.4.2 (r22196)\n'
不过,查看 svnmerge.py 代码,我注意到对于 python 版本 2.4 及更高版本,它遵循不同的执行方式小路。 而不是调用 os.popen4() 它使用 subprocess.Popen()。 尝试重现该错误:
C:\>python Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import subprocess >>> p = subprocess.Popen("svn --version", stdout=subprocess.PIPE, >>> close_fds=False, stderr=subprocess.PIPE) Traceback (most recent call last): File "", line 1, in File "C:\Python25\lib\subprocess.py", line 594, in __init__ errread, errwrite) File "C:\Python25\lib\subprocess.py", line 816, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified >>>
目前,我已注释掉 2.4 及更高版本的特定代码,但我想找到合适的解决方案。
如果不是很明显,我是一个完全的Python新手,但谷歌没有提供帮助。
I'm trying to use svnmerge.py
to merge some files. Under the hood it uses python, and when I use it I get an error - "The system cannot find the file specified". Colleagues at work are running the same version of svnmerge.py
, and of python (2.5.2, specifically r252:60911) without an issue.
I found this link, which describes my problem. Trying what was outlined there, I confirmed Python could find SVN (it's in my path):
P:\>python Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> i,k = os.popen4("svn --version") >>> i.close() >>> k.readline() 'svn, version 1.4.2 (r22196)\n'
Looking at the svnmerge.py
code, though, I noticed for python versions 2.4 and higher it was following a different execution path. Rather than invoking
os.popen4() it uses subprocess.Popen(). Trying that reproduces the error:
C:\>python Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import subprocess >>> p = subprocess.Popen("svn --version", stdout=subprocess.PIPE, >>> close_fds=False, stderr=subprocess.PIPE) Traceback (most recent call last): File "", line 1, in File "C:\Python25\lib\subprocess.py", line 594, in __init__ errread, errwrite) File "C:\Python25\lib\subprocess.py", line 816, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified >>>
For now, I've commented out the 2.4-and-higher specific code, but I'd like to find a proper solution.
If it's not obvious, I'm a complete python newbie, but Google hasn't helped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误,请参阅
subprocess.Popen
的文档。 要么需要有一个“shell=True”选项,要么第一个参数必须是一个序列['svn', '--version']
。 现在,Popen
正在寻找一个名为“svn --version”的可执行文件,但它没有找到。我不知道为什么它会为你的同事工作,如果他们运行相同的操作系统和Python版本...FWIW它在Mac上给了我相同的错误消息,我提供的两种方法之一修复了它。
It's a bug, see the documentation of
subprocess.Popen
. There either needs to be a"shell=True
" option, or the first argument needs to be a sequence['svn', '--version']
. As it is now,Popen
is looking for an executable named, literally, "svn --version" which it doesn't find.I don't know why it would work for your colleagues though, if they are running the same OS and version of Python... FWIW it gives me the same error message on a mac, and either of the two ways I gave fixes it.