1

I'm trying to call a command line with Popen.

The command line operates in the shell

cd C:\Program Files (x86)\Inventor
Inventor -exe -- no-gui -f C:\Users\Vince\Documents\Inventor\Inventor.iam

But when I try this with Popen in my program

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")

It returns me FileNotFoundError: [WinError 2]

So I can't figure out what is wrong with the file path.

Vincent
  • 113
  • 2
  • 14

2 Answers2

1

add (shell =True) in argument

but use with caution warning for using shell = True argument

aptro
  • 51
  • 3
  • 1
    It doesn't work. It's better with \\ but I think the problem comes from the arguments `"Inventor -exe -- no-gui -f "` – Vincent Jun 30 '15 at 10:09
0

You could try to add the full path to the executable:

#!/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)

See Popen with conflicting executable/path, to understand how the executable is search with and without shell=True.

To avoid hardcoding paths to special Windows folders, see Python, get windows special folders for currently logged-in user.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670