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 #102 of 127 |
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







Thu Sep 8, 2005 9:57 pm

p0lyglut
Online Now Online Now
Send Email Send Email

Forward
Message #102 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