简体   繁体   中英

Running Another program from python

I want to call a program multiple times from a python code, and save the output of that program in a text file. My first problem right now is just calling the other code. I have to redirect to a different directory and call ./rank on output.txt. This is how Im trying to do it:

    TheCommand = "~/src/rank-8-9-2011/rank output.txt"
    os.system(TheCommand)

but im getting a parsing error.

 [Parsing error on line ]Unknown error: 0

Im running python2.7 on Mac OS 10.5.8. Im not sure what the problem is. I also tried using subprocess:

 subprocess.call(["~/src/rank-8-9-2011/rank", "output.txt"])

This does not find the directory (I have a feeling Im using the subprocess incorrectly), but I dont know what is wrong with the os.system.

the name of the program in the first argument to subprocess.Popen must not contain ~ as it doesn't pass the string to the shell for processing (which like always using parameterized queries in sql, protects one from string injection attacks, eg if instead of output.text one had ;rm -rf / , the system version would run rank and then run rm -rf . but the subprocess.Popen would only have rank open a file named ;rm -rf . ), so one should expand it by calling os.path.expanduser :

subprocess.Popen([os.path.expanduser('~/src/rank-8-9-2011/rank'), "output.txt"])

although it is possible to turn shell processing on by passing shell=True , it is not recommended for the aforementioned reason.

you should try http://docs.python.org/library/os.path.html#os.path.expanduser

import os.path
subprocess.call([os.path.expanduser("~/src/rank-8-9-2011/rank"), "output.txt"])

I'm fairly certain your parsing error is coming from rank , not from your os.system command, as nothing there looks weird. What happens if you run rank manually?

subprocess seems to have a problem with '~', although I'm not immediately sure why. Put the full path and it should work (although you'll likely get that parsing error if it is indeed a problem with rank ).

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