1

Other scripts are involved so I don't know how to ask this with all of the tino.

Right now I have this command,

 subprocess.call(['python2.7', 'cello_client.py', 'get_results',
                  '--jobid','pythonTest4', '--filename',
                  'pythonTest4_dnacompiler_output.txt','>','out.txt'])

It's suppoosed to push the output to a text file out.txt, but it doesn't do this. Any suggestions?

martineau
  • 119,623
  • 25
  • 170
  • 301
J. Smith
  • 143
  • 1
  • 2
  • 11

1 Answers1

1

The > is a shell redirection command but you didn't run through a shell. You can do what the shell would do: open the file and attach it as the program's stdout.

subprocess.call(['python2.7', 'cello_client.py', 'get_results',
    '--jobid','pythonTest4', '--filename',
    'pythonTest4_dnacompiler_output.txt'],
    stdout=open('out.txt', 'wb'))

You can read stdout directly into memory with a different subprocess command:

proc = subprocess.Popen(['python2.7', 'cello_client.py', 'get_results',
    '--jobid','pythonTest4', '--filename',
    'pythonTest4_dnacompiler_output.txt'])
out_text, err_text = proc.communicate()
return_code = proc.returncode
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • This works great, thanks so much! I have a followup question. as soon as I write to this text file, I open it and read it in, then close the file. Is there a way to edit the above command to simply save the output as a string in my program? – J. Smith Mar 19 '17 at 05:57
  • Updated to show `Popen.communicate` – tdelaney Mar 19 '17 at 16:04