我有一段代码接受用户提供的可执行文件或批处理文件的路径。我拆分路径以获取文件名和路径(尾部和头部),并将头部用作新进程的当前工作目录。如果路径不包含目录,则可以正常工作。但是,如果确实如此,我会收到以下错误:
WindowsError: [Error 2] The system cannot find the file specified
示例代码:
def poop(self, path_to_program):
head, tail = path.split(path_to_program)
if(head.startswith('"')):
head = head.strip('"')
if(tail.endswith('"')):
tail = tail.strip('"')
if(head<>''):
p = Popen(tail, cwd=head, shell=False)
else:
p = Popen(tail)
如果path_to_program
没有拆分并直接传递给 Popen,它可以正常工作。
不知道出了什么问题。