Issue combining gzip and subprocess

Piet van Oostrum piet at cs.uu.nl
Wed Jul 22 17:50:03 EDT 2009


>>>>> Scott David Daniels <Scott.Daniels at Acm.Org> (SDD) schreef:

>SDD> Piet van Oostrum wrote:
>>> ...
>>> f = gzip.open(filename, 'w')
>>> proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE)
>>> while True:
>>> line = proc.stdout.readline()
>>> if not line: break
>>> f.write(line)
>>> f.close()

>SDD> Or even:
>SDD>     proc = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE)
>SDD>     with gzip.open(filename, 'w') as dest:
>SDD>         for line in iter(proc.stdout, ''):
>SDD>             f.write(line)

If it would work.

1) with gzip... is not supported in Python < 3.1
2) for line in iter(proc.stdout), i.e. no second argument.
3) dest <==> f should be the same identifier.

Lesson: if you post code either: 
- test it and copy verbatim from your test, or
- write a disclaimer 
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list