[Tutor] pipes and redirecting

Adam Gold awg1 at gmx.com
Tue May 27 22:01:51 CEST 2014


I'm trying to run the following unix command from within Python as
opposed to calling an external Bash script (the reason being I'm doing
it multiple times within a for loop which is running through a list):

"dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync | pbzip2 > 1.img.bz2"

The first thing I do is break it into two assignments (I know this isn't
strictly necessary but it makes the code easier to deal with):

ddIf = shlex.split("dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync")
compress = shlex.split("pbzip2 > /home/adam/1.img.bz2")

I have looked at the docs here (and the equivalent for Python 3)
https://docs.python.org/2/library/subprocess.html.  I can get a 'simple'
pipe like the following to work:

p1 = subprocess.Popen(["ps"], stdout=PIPE)
p2 = subprocess.Popen(["grep", "ssh"], stdin=p1.stdout,
stdout=subprocess.PIPE)
p1.stdout.close()
output = p2.communicate()[0]


I then try to adapt it to my example:

p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE)
p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output = p2.communicate()[0]

I get the following error:

pbzip2: *ERROR: File [>] NOT found!  Skipping...
-------------------------------------------
pbzip2: *ERROR: Input file [/home/adam/1.img.bz2] already has a .bz2
extension!  Skipping

I think that the '>' redirect needs to be dealt with using the
subprocess module as well but I can't quite put the pieces together.
I'd appreciate any guidance.  Thanks.



More information about the Tutor mailing list