Search the web
Sign In
New User? Sign Up
perl-python
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
making system calls   Message List  
Reply | Forward Message #103 of 127 |
Re: [perl-python] making system calls

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


Fri Sep 9, 2005 7:18 pm

orionrobots@...
Send Email Send Email

Forward
Message #103 of 127 |
Expand Messages Author Sort by Date

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...
xah lee
p0lyglut
Online Now Send Email
Sep 8, 2005
9:58 pm

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...
danny staple
orionrobots@...
Send Email
Sep 9, 2005
10:56 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help