Steve Grafton
Steve Grafton

Reputation: 1951

Calling a Python code through another Python code

So I have a python code, called say program1.py

To execute it normally I do the following

python program1.py -i input.dat -o output.dat

-i indicates what follows is an input file (in this case input.dat)
-o indicates the name of the output file to be written (in this case output.dat)

I tried to call the program (program1.py) within another python code like this:

os.system('/sw/bin/python2.7 program1.py -i Users/Steve/Desktop/{}   -o  Users/Steve/Desktop/{}'.format(inputFile),.format(outputFile))

This does not seem to work. I know I have to use the OS command but the exact way to format for "-i" and "-o" is puzzling.

Can anyone show me what I am doing wrong?

Upvotes: 0

Views: 270

Answers (2)

abarnert
abarnert

Reputation: 365697

The actual problem in your code is that you're not using format correctly. In fact, as written, it won't even compile:

os.system('/sw/bin/python2.7 program1.py -i Users/Steve/Desktop/{}   -o  Users/Steve/Desktop/{}'.format(inputFile),.format(outputFile))

For the first argument to system, you're trying to format a string with two placeholders and only feeding it one value. Then, for the second argument, you're passing .format(outputFile), which is an error.

You can fix all that by writing:

os.system('/sw/bin/python2.7 program1.py -i Users/Steve/Desktop/{}   -o  Users/Steve/Desktop/{}'.format(inputFile, outputFile))

However, this is a bad way to do things. Usually, you can just trivially rewrite your code to import the functionality into your script and call it. If the only reason you're not doing that is to force a child process, multiprocessing is still usually a better way to do that.


But sometimes you do need to do something like this. In that case, you should still not use os.system. First, to run a new script using the same interpreter as your script, use sys.executable rather than hardcoding it. Second, as the os.system docs or help will tell you, you should almost always be using functions in subprocess instead. So:

subprocess.call([sys.executable, 'program1.py',
                 '-i', 'Users/Steve/Desktop/{}'.format(inputfile),
                 '-o', 'Users/Steve/Desktop/{}'.format(outputfile)])

This takes care of all kinds of problems for you—e.g., you don't have to figure out how to quote the filenames in case they have spaces—and it's safer and more efficient to not use the shell if you don't need it for anything.


However, this still probably won't work for you, unless your current working directory happens to be '/'. If you want to use an absolute path, you have to start it with a /.

While we're at it, it's generally better to use path functions instead of string functions to deal with paths, as in os.path.join('/Users/Steve/Desktop', inputfile).


Even better, you might want to look up the current user's desktop instead of hardcoding that path. The way Apple recommends doing this requires installing pyobjc (which comes built-in with Apple's Python installation, but probably doesn't with the Fink installation you're using instead). But then it's as simple as:

desktop = AppKit.NSSearchPathForDirectoriesInDomains(AppKit.NSDesktopDirectory,
                                                     AppKit.NSUserDomainMask, 
                                                     True)[0]

Upvotes: 4

Arman_Vasilyan
Arman_Vasilyan

Reputation: 63

I think,you should import the program1 module into your filename.py and use program1 functions on it

Upvotes: 4

Related Questions