开发者

Problem when invoking command prompt from a python script

开发者 https://www.devze.com 2023-03-02 22:45 出处:网络
I would like some help towards invoking a command prompt (& passing some argument to the command prompt) from a python script.

I would like some help towards invoking a command prompt (& passing some argument to the command prompt) from a python script.

I use pyqt4 for developing the UI and on the UI I have a run button. On selection of run button, I would like to invoke a command prompt and pass on some script name as the argument.

self.connect(run_button, SIGNAL('clicked()'), self.runscript) # this is my run button  signal and i'm calling the runscript() 

def runscript(self):
    print 'Inside Run Script'
    os.chdir('C:\PerfLocal_PAL')
    try:
        subprocess.call(['C:\windows\system32\cmd.exe'])
    except:
        print 'Exception Caused.'

When I click on run button, the application dies and it does not invoke the command prompt at all. I tried with os开发者_C百科.system as well same result.

also, I would like to know how to pass the argument to the call function?

Any help towards this is highly appreciated.

Thanks,


To correctly define file paths in Python on Windows, you need to do one of three things:

  • Use forward slashes: "C:/PerfLocal_PAL" (Python understands forward slashes regardless of platform)
  • Use raw strings: r"C:\PerfLocal_PAL"
  • Escape the backslashes: "C:\\PerfLocal_PAL"

This affects both your chdir call and your subprocess.call invocation.

However, you will also have trouble due to the fact that your parent process is a GUI application, and hence has no console streams for stdin, stdout and stderr. Try using the following instead to get a completely separate command window:

subprocess.call("start", shell=True)

You may also want to use the "/D" argument of start to set your working directory, rather than changing the cwd of the parent process:

subprocess.call(["start", "/DC:\\PerfLocal_PAL"], shell=True)


Have you tried debugging this at all? Which line does the script fail on? Does it actually start the runscript function at all?

Regarding passing arguments to cmd.exe, have a look at the documentation for subprocess.call. It will show you that you can have a second argument providing the command line parameters to the program, e.g.

subprocess.call(["C:\windows\system32\cmd.exe", "scriptname.bat"])


One problem is that subprocess.call will block until it is complete, and cmd.exe will not return until you exit it. That answers the 'just dies' but may not explain the console never appearing. Start with this:

subprocess.Popen(['C:\Windows\system32\cmd.exe'])

That at least will not block. If you can get it to appear, try your arguments, like this:

subprocess.Popen(['C:\Windows\system32\cmd.exe', 'program_or_script', 'arg1'])


Your signal connection and your subprocess call seems to be fine. Change your chdir call to:

  os.chdir(r'C:\PerfLocal_PAL')

I guess the error you are getting is of the form (when you launch your application from the command prompt):

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\PerfLocal_PAL'
0

精彩评论

暂无评论...
验证码 换一张
取 消