0

I want to write a python script that pipes a value of a variable (e.g. var = 5) into an external program. I want to store that output then in a .txt file. Since I want to run a couple of these processes simultaneously I tried to do this using subprocess. But I failed. Can someone help me?

Thanks a lot!

4
  • 1
    Edit your post and add the code ... Commented Jan 31, 2016 at 20:00
  • Possible duplicate Commented Feb 1, 2016 at 12:23
  • Something like this? from subprocess import check_output # ls command out = check_output(["ls", "-als"]) # open the empty file logfilewriteout = open("/Users/youruser/Desktop/test.txt", 'w') logfilewriteout.write(out) logfilewriteout.close() Commented Feb 1, 2016 at 12:44
  • Many questions here: 1. How to pass var's value to a subprocess (too broad: how do you want to pass it: as a command-line parameter Popen(['command', str(var)], ...), or via subprocess' stdin e.g., as var's text ascii representation print(var, file=process.stdin), or using some other IPC method?) 2. How to redirect subprocess' stdout to a file 3. How to run subprocesses in parallel. Please, limit your question to one issue per question. Commented Feb 1, 2016 at 20:31

0

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.