Python System Calls

From Wiki
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

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)