[Tutor] pipes and redirecting

Alan Gauld alan.gauld at btinternet.com
Wed May 28 10:29:42 CEST 2014


On 27/05/14 21:01, Adam Gold wrote:

> "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

And that's the start of the problem because it should be three:
The first command, the second command and the output file.

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

compress = "pbzip2"
outfile = open('/home/adam/1.img.bz2','w')

The redirection symbol is not something subprocess can
use as an argument.

> p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE)
> p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE)

Use the output file here.

p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=outfile)


> 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.

Alternatively read the output into a variable using communicate but then 
write it out to the file manually at the end.

[You might also be able to use shell=TRUE but that introduces other 
issues. But I don't know whether using shell includes redirection.]

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list