我正在尝试用Popen调用命令行。
命令行在shell中运行
cd C:\Program Files (x86)\Inventor
Inventor -exe -- no-gui -f C:\Users\Vince\Documents\Inventor\Inventor.iam
但是当我在程序中尝试使用Popen时
from subprocess import *
from os.path import expanduser
home = expanduser("~")
path = home + "\\Documents\\Inventor\\Inventor.iam"
cmd = "Inventor -exe -- no-gui -f " + path
Popen(cmd, cwd="C:\\Program Files (x86)\\Inventor")
它返回FileNotFoundError: [WinError 2]
所以我无法弄清楚文件路径有什么问题。
答案 0 :(得分:1)
在参数
中添加(shell = True)答案 1 :(得分:0)
您可以尝试添加可执行文件的完整路径:
#!/usr/bin/env python
import os.path
import subprocess
exedir = os.path.join(os.environ['PROGRAMFILES'], "Inventor")
exepath = os.path.join(exedir, "Inventor.exe")
filepath = os.path.join(os.path.expanduser('~'), r'Documents\Inventor\Inventor.iam')
subprocess.check_call([exepath, '-exe', '--', 'no-gui', '-f', filepath],
cwd=exedir)
请参阅Popen with conflicting executable/path,了解使用和不使用shell=True
搜索可执行文件的方式。
要避免将特定Windows文件夹的路径硬编码,请参阅Python, get windows special folders for currently logged-in user。