Python Forum
How can python handle a stdout from bash shell by using Popen() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can python handle a stdout from bash shell by using Popen() (/thread-6596.html)



How can python handle a stdout from bash shell by using Popen() - ruifenghu - Nov-29-2017


I am writing python to Popen a bash script, the script creates a tar file, I want to tar directly to stdout without an actual output file, then use python to get the tar content, and then write it into a *.tar.gz file in python. how can I get that?

The shell script named mktarfile.sh, and the command in it is:
Quote:cd "$tmpdir" && tar cf - .

And the Python code is:
Quote:x_return = os.popen('mktarfile.sh')



RE: How can python handle a stdout from bash shell by using Popen() - RickyWilson - Dec-01-2017

I would use the tarfile and gzip modules that come built-in to python. What is the purpose of the script if you don't mind me asking?


RE: How can python handle a stdout from bash shell by using Popen() - wavic - Dec-01-2017

You can't. The tar program will create the archive and the output is what you will be able to collect. One way is to create a storage into memory, mount it like a disk drive and tell tar to create the the archive there. But again, there will be a file.


RE: How can python handle a stdout from bash shell by using Popen() - nilamo - Dec-01-2017

If you use subprocess instead of os, you can pass subprocess.PIPE to the stdout parameter, and then process whatever tar's output is.