Hi there,
You could save recompressing an intermediate file on a unix system by
changing your input to the gzip commnad to include the -c option
This will then place the decompressed file on stdout. You can then
just pipe stdout from this to the stdin of the tail command (if you
dont pass a file to tail it will expect its input on stdin). You
should probably remove the wait as well, and only once you have
started both processes, should you wait for them to finish.
This small saving would speed up a program a little, and save an
intermediate file being created - so it has fewer side effects, fewer
disk writes etc.
import subprocess
# -*- coding: utf-8 -*-
# Python
gzip_process = subprocess.Popen([r"gzip","-cd", "x.txt.gz", stdout = PIPE])
tail_process = subprocess.Popen([r"tail","-n 1"], stdin =
gzip_process.stdout, stdout=PIPE)
last_line = tail_process.communicate()[0]
print last_line
Danny
--
http://orionrobots.co.uk - Build Robots
On 08/09/05, xah lee <xah@...> wrote:
> 200509: Making System Calls (with unix and Python 2.4)
>
> To make a system call, use:
>
> subprocess.Popen([r"gzip","-d", "x.txt.gz"]).wait()
>
>
> The subprocess module is available only since Python 2.4, and it is
> intended to replace several other older ones:
>
> os.system
> os.spawn*
> os.popen*
> popen2.*
> commands.*
>
>
> The subprocess.Popen([...]) creates a object. The .wait() makes the
> code wait until the system call finished. To not wait for it, simply
> use:
>
> subprocess.Popen([r"gzip","-d", "x.txt.gz"])
>
>
> To make a system call and get its output, do, for example:
>
> last_line=subprocess.Popen([r"tail","-n 1", "x.txt"],
> stdout=subprocess.PIPE).communicate()[0]
>
>
> The "communicate()" automatically makes the call wait.
>
> In the following example, we make a system call to decompress a gzip
> file (and wait for it), then get the last line of the file using
> "tail", then gzip it again.
>
> # -*- coding: utf-8 -*-
> # Python
>
> import subprocess
>
> subprocess.Popen([r"gzip","-d", "x.txt.gz"]).wait()
>
> last_line=subprocess.Popen([r"tail","-n 1", "x.txt"],
> stdout=subprocess.PIPE).communicate()[0]
>
> subprocess.Popen([r"gzip","x.txt"]).wait()
>
> print last_line
>
>
> Reference:
> http://www.python.org/doc/2.4/lib/module-subprocess.html
> --------
> this post is archived at
> http://xahlee.org/perl-python/system_calls.html
>
>
>
> ☄
>
>
> ________________________________
> YAHOO! GROUPS LINKS
>
>
> Visit your group "perl-python" on the web.
>
> To unsubscribe from this group, send an email to:
> perl-python-unsubscribe@yahoogroups.com
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
>
> ________________________________
>