Python System Calls

From Wiki
Revision as of 20:16, 14 September 2011 by Scott (talk | contribs) (subprocess module (Python 2.4+))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 of a terminal-interactive program:

import subprocess
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)