Python-Windows-Popen(shlex.split(command),shell = False导致OSError:[Errno 2]没有这样的文件或目录 [英] Python - Windows - Popen(shlex.split(command), shell=False causes OSError: [Errno 2] No such file or directory
问题描述
我正在运行此代码,该代码在OSX上运行正常,但在Windows上导致错误:
I am running this code which works fine in OSX but causes an error on Windows:
command = "C:\\progra~2\\itms\\iTMSTransporter -m verify -f /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp -u username -p password -o /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt -s provider -v eXtreme"
self.process1 = Popen(shlex.split(command), shell=False, stdin=PIPE)
我在Windows上收到的错误是:
The error I am recieving on Windows is:
WindowsError: [Error 2] The system cannot find the file specified
为什么在Windows上会给我这个错误?
Why is it giving me this error on Windows?
推荐答案
您的shlex.split()
由于删除\字符而破坏了路径.让我们检查一下:
Your shlex.split()
destroys your path because of removing \ characters. Let's check:
import shlex
command = "C:\\progra~2\\itms\\iTMSTransporter -m verify -f Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp -u username -p password -o /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt -s provider -v eXtreme"
print shlex.split(command)
['C:progra~2itmsiTMSTransporter', '-m', 'verify', '-f', '/Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp', '-u', 'username', '-p', 'password', '-o', '/Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt', '-s', 'provider', '-v', 'eXtreme']
如您所见,可执行文件的路径不正确(C:progra~2itmsiTMSTransporter
),因此Popen找不到它.
As you can see, path to executable is incorrect (C:progra~2itmsiTMSTransporter
), so Popen can't find it.
将路径分隔符更改为/
,这在两种Linux/Windows环境中都是安全的:
Change your path separator to /
, which is safe in both Linux/Windows environments:
command = "C:/progra~2/itms/iTMSTransporter -m verify -f Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp -u username -p password -o /Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt -s provider -v eXtreme"
print shlex.split(command)
['C:/progra~2/itms/iTMSTransporter', '-m', 'verify', '-f', 'Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp', '-u', 'username', '-p', 'password', '-o', '/Volumes/Stuff/Temp/TMP_S_0_V_TV2.itmsp/LOGFILE.txt', '-s', 'provider', '-v', 'eXtreme']
Popen()
将正确处理此路径.
这篇关于Python-Windows-Popen(shlex.split(command),shell = False导致OSError:[Errno 2]没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!