具有多个参数,变量和当前工作目录的命令行

时间:2015-06-30 09:13:25

标签: python shell python-3.x command-line command-line-arguments

我正在尝试用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]

所以我无法弄清楚文件路径有什么问题。

2 个答案:

答案 0 :(得分:1)

在参数

中添加(shell = True)

但请谨慎使用 warning for using shell = True argument

答案 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