简体   繁体   中英

Python: How to pass this chain of commands at once to subprocess.call()

I'm trying to pass these below chained commands at once to the subprocess .But it's throwing error.

Command to pass:

"C:\MyScript\run.bat" -I"C:\MyScript" -- "C:\MyScript\MyScript1.pl" "file_name"

where, file_name is a variable which holds the name of a file.

Code:

cmd = '"C:\MyScript\run.bat" -I"C:\MyScript" -- "C:\MyScript\MyScript1.pl"' + " " + filename
subprocess.call(cmd)

Error:

Traceback (most recent call last):
File "c:\Test\WWX_2.py", line 28, in
<module>
subprocess.call(cmd)
File "C:\Python26\lib\subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python26\lib\subprocess.py", line 595, in __init__
errread, errwrite)
File "C:\Python26\lib\subprocess.py", line 821, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified 

Use raw string literals to prevent \\r from being interpreted as a carriage return. The space-only arguments are also passed as actual command line arguments, but you probably don't want that to happen, so remove them.

cmd = [r'C:\MyScript\run.bat', r'-I"C:\MyScript"', '--', r'C:\MyScript\MyScript1.pl', filename]
subprocess.call(cmd)

您必须转义路径中的所有反斜杠,即

C:\\whatever\\batch.bat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM