2

I have a nonpython program I am running with python using the os.system command, but I put this command inside a function. The program I want to run with os.system is supposed to give me an output file, and I need that output for processing, also I need that output to be actually written in the directory I am sending it to.

I wrote my function is the following general format

def myFunction(infile):
    os.system('myProgram '+infile+' '+outfileName)
    outfile = numpy.loadtxt(outfileName)
    return outfile

However, the output of myProgram (outfileName) isn't being written to my directory and numpy can't therefore load it. Is there a way to store globally outputs of programs I run using os.system when it's inside a function?

8
  • 1
    What is outfileName? Is that variable in the scope of this function? Commented Apr 19, 2017 at 17:14
  • 1
    You may need to use an absolute path for outfileName to make sure it's where you want it to be. Or, can you have myProgram output to STDOUT? Commented Apr 19, 2017 at 17:15
  • Even with absolute paths, it is not writing to my directory Commented Apr 19, 2017 at 17:22
  • 1
    Do you run this python program from the command line or double-click it? Do the input and output files have spaces in them? Did the program work... or does it error? I did os.system('cp test test2') and it worked fine. Can you substitute something like that and try it for sanity testing. Also, print(repr('myProgram '+infile+' '+outfileName)) and post that here for us to see. And of course... retval = os.system('myProgram '+infile+' '+outfileName) and test the program for a nonzero return code! Commented Apr 19, 2017 at 17:35
  • myProgram is able to run, as in addition to storing data into outfileName it also prints to screen additional data. The additional data I can see, but the data I want isn't writing to the directory I want it to be. Commented Apr 19, 2017 at 17:44

1 Answer 1

2

Assuming myProgram is working correctly, this is likely happening because myProgram does not know the python path, so the file is simply being written somewhere else. Try using the full paths and see if that works.

Assuming infile and outfileName are relative paths in your current working directory, you could do:

def myFunction(infile):
    cmd  = 'myProgram ' + os.path.join(os.getcwd(), infile)
    cmd += ' ' + os.path.join(os.getcwd(), outfileName))
    os.system(cmd)
    outfile = numpy.loadtxt(outfileName)
    return outfile
Sign up to request clarification or add additional context in comments.

3 Comments

What is "the python path" and how is it related to the problem? os.system uses the same current working directory as the program calling it. Joining with getcwd() is pointless, relative paths already work that way. Relative paths can be a problem if you are running the program by double-clicking it or running it as a daemon. But then, getcwd() would continue to be a problem.
Ah, good point. So then the culprit must be something in myProgram
Quite likely. He may be double-clicking from the desktop (so cwd is bad) or using spaces in the filenames. But I'm voting for something wrong with myprogram... and he really should be testing its return code.

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.