Python System Calls: Difference between revisions

From Wiki
Jump to navigation Jump to search
Created page with '== Backquotes == <source lang="python"> output = `dmesg | grep hda` </source> == os.popen == <source lang="python"> import os cmd = "medcon -f %s" % fileName output = os.popen(c…'
 
Line 20: Line 20:
for line in output:
for line in output:
     #do something with line
     #do something with line
</source>
An example
<source lang="python">
command = "list5 12345"
p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(output, error) = p.communicate('s')  # 's' tells it to stop
</source>
</source>



Revision as of 20:15, 14 September 2011

Backquotes

output = `dmesg | grep hda`

os.popen

import os
cmd = "medcon -f %s" % fileName
output = os.popen(cmd)
for line in output:
    #do something with line

subprocess module (Python 2.4+)

from subprocess import *
cmd = "medcon -f %s" % fileName
output = Popen(cmd, shell=True, bufsize=512, stdout=PIPE).stdout
for line in output:
    #do something with line

An example

command = "list5 12345"
p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(output, error) = p.communicate('s')  # 's' tells it to stop

The os.shutil module

Used for copying, moving, and deleting files and directories:

os.listdir(path)