Python Forum
How can python handle a stdout from bash shell by using Popen()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can python handle a stdout from bash shell by using Popen()
#1

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')
Reply
#2
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?
Reply
#3
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
If you use subprocess instead of os, you can pass subprocess.PIPE to the stdout parameter, and then process whatever tar's output is.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [subprocess] Why stdout sent to stderr? Winfried 3 421 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Help creating shell scrip for python file marciokoko 10 1,258 Sep-16-2023, 09:46 PM
Last Post: snippsat
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Performance options for sys.stdout.writelines dgrunwal 11 2,996 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  batch file for running python scipt in Windows shell MaartenRo 2 1,825 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Call a bash script from within a Python programme Pedroski55 6 2,377 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Showing and saving the output of a python file run through bash Rim 3 2,373 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,570 May-04-2021, 08:58 PM
Last Post: liudr
  changing stdout and stderr Skaperen 4 2,645 Dec-02-2020, 08:58 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020