tptptpfg
tptptpfg

Reputation: 51

Using os.system() inside a function

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?

Upvotes: 2

Views: 833

Answers (1)

Nolan Conaway
Nolan Conaway

Reputation: 2757

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

Upvotes: 2

Related Questions