2

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.

3 Answers 3

5

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, e.g. 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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.