Python OS Module | OS Module in Python

The OS module in Python provides a unified interface to a number of operating system functions. Most of the functions in this module are implemented by platform-specific modules, such as posix and nt. The os module automatically loads the right implementation module when it is first imported.

Working with files using OS Module in Python

The built-in open function in Python OS Module lets you create, open, and modify files. This module adds those extra functions you need to rename and remove files.

Example: Using the os module to rename and remove files:

import os
import string

def replace(file, search_for, replace_with):
    # replace strings in a text file

    back = os.path.splitext(file)[0] + ".bak"
    temp = os.path.splitext(file)[0] + ".tmp"

    try:
        # remove old temp file, if any
        os.remove(temp)
    except os.error:
        pass

    fi = open(file)
    fo = open(temp, "w")

    for s in fi.readlines():
        fo.write(string.replace(s, search_for, replace_with))

    fi.close()
    fo.close()

    try:
        # remove old backup file, if any
        os.remove(back)
    except os.error:
        pass

    # rename original to backup...
    os.rename(file, back)

    # ...and temporary to original
    os.rename(temp, file)

#
# try it out!

file = "samples/sample.txt"

replace(file, "hello", "tjena")
replace(file, "tjena", "hello")

Working with directories using OS Module in Python

The OS module in Python also contains a number of functions that work on entire directories.

The listdir function returns a list of all filenames in a given directory. The current and parent directory markers used on Unix and Windows (. and ..) are not included in this list.

Example: Using the os module to list the files in a directory:

import os

for file in os.listdir("samples"):
    print file
Output: sample.au
sample.jpg
sample.wav

The getcwd and chdir functions are used to get and set the current directory:

Example: Using the os module to change the working directory:

# File: os-example-4.py

import os

# where are we?
cwd = os.getcwd()
print "1", cwd

# go down
os.chdir("samples")
print "2", os.getcwd()

# go back up
os.chdir(os.pardir)
print "3", os.getcwd()
Output: 1 /ematter/librarybook
2 /ematter/librarybook/samples
3 /ematter/librarybook

The makedirs and removedirs functions are used to create and remove directory hierarchies.

Example: Using the os module to create and remove multiple directory levels:


import os

os.makedirs("test/multiple/levels")

fp = open("test/multiple/levels/file", "w")
fp.write("inspector praline")
fp.close()

# remove the file
os.remove("test/multiple/levels/file")

# and all empty directories above it
os.removedirs("test/multiple/levels")

Note that removedirs removes all empty directories along the given path, starting with the last directory in the given path name. In contrast, the mkdir and rmdir functions can only handle a single directory level.

Example: Using the os module to create and remove directories:

# File: os-example-7.py

import os

os.mkdir("test")
os.rmdir("test")

os.rmdir("samples") # this will fail

Working with file attributes using OS Module

The stat function fetches information about an existing file. It returns a 9-tuple that contains the size, inode change timestamp, modification timestamp, and access privileges.

Example: Using the os module to get information about a file:

# File: os-example-1.py

import os
import time

file = "samples/sample.jpg"

def dump(st):
    mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
    print "- size:", size, "bytes"
    print "- owner:", uid, gid
    print "- created:", time.ctime(ctime)
    print "- last accessed:", time.ctime(atime)
    print "- last modified:", time.ctime(mtime)
    print "- mode:", oct(mode)
    print "- inode/dev:", ino, dev

#
# get stats for a filename

st = os.stat(file)

print "stat", file
dump(st)
print

#
# get stats for an open file

fp = open(file)

st = os.fstat(fp.fileno())

print "fstat", file
dump(st)
Output: stat samples/sample.jpg
– size: 4762 bytes
– owner: 0 0
– created: Tue Sep 07 22:45:58 1999
– last accessed: Sun Sep 19 00:00:00 1999
– last modified: Sun May 19 01:42:16 1996
– mode: 0100666
– inode/dev: 0 2

fstat samples/sample.jpg
– size: 4762 bytes
– owner: 0 0
– created: Tue Sep 07 22:45:58 1999
– last accessed: Sun Sep 19 00:00:00 1999
– last modified: Sun May 19 01:42:16 1996
– mode: 0100666
– inode/dev: 0 0

Some fields don’t make sense on non-Unix platforms; for example, the (inode, dev) tuple provides a unique identity for each file on Unix, but can contain arbitrary data on other platforms.

The stat module contains a number of useful constants and helper functions for dealing with the members of the stat tuple. Some of these are shown in the examples below.

You can modify the mode and time fields using the chmod and utime functions:

Example: Using the os module to change a file’s privileges and timestamps:

import os
import stat, time

infile = "samples/sample.jpg"
outfile = "out.jpg"

# copy contents
fi = open(infile, "rb")
fo = open(outfile, "wb")

while 1:
    s = fi.read(10000)
    if not s:
        break
    fo.write(s)

fi.close()
fo.close()

# copy mode and timestamp
st = os.stat(infile)
os.chmod(outfile, stat.S_IMODE(st[stat.ST_MODE]))
os.utime(outfile, (st[stat.ST_ATIME], st[stat.ST_MTIME]))

print "original", "=>"
print "mode", oct(stat.S_IMODE(st[stat.ST_MODE]))
print "atime", time.ctime(st[stat.ST_ATIME])
print "mtime", time.ctime(st[stat.ST_MTIME])

print "copy", "=>"
st = os.stat(outfile)
print "mode", oct(stat.S_IMODE(st[stat.ST_MODE]))
print "atime", time.ctime(st[stat.ST_ATIME])
print "mtime", time.ctime(st[stat.ST_MTIME])
Output: original =>
mode 0666
atime Thu Oct 14 15:15:50 1999
mtime Mon Nov 13 15:42:36 1995
copy =>
mode 0666
atime Thu Oct 14 15:15:50 1999
mtime Mon Nov 13 15:42:36 1995

Working with processes in OS Module

The system function runs a new command under the current process, and waits for it to finish.

Example: Using the os module to run an operating system command:

import os

if os.name == "nt":
    command = "dir"
else:
    command = "ls -l"

os.system(command)
Output: -rwxrw-r– 1 effbot effbot 76 Oct 9 14:17 README
-rwxrw-r– 1 effbot effbot 1727 Oct 7 19:00 SimpleAsyncHTTP.py
-rwxrw-r– 1 effbot effbot 314 Oct 7 20:29 aifc-example-1.py
-rwxrw-r– 1 effbot effbot 259 Oct 7 20:38 anydbm-example-1.py

The command is run via the operating system’s standard shell, and returns the shell’s exit status.

Using the os module to start a new process

import os
import sys

program = "python"
arguments = ["hello.py"]

print os.execvp(program, (program,) +  tuple(arguments))
print "goodbye"

Python provides a whole bunch of exec functions, with slightly varying behavior. The above example uses execvp, which searches for the program along the standard path, passes the contents of the second argument tuple as individual arguments to that program, and runs it with the current set of environment variables. 

Using the os module to run another program (Unix)

import os
import sys

def run(program, *args):
    pid = os.fork()
    if not pid:
        os.execvp(program, (program,) +  args)
    return os.wait()[0]

run("python", "hello.py")

print "goodbye"

The fork returns zero in the new process (the return from fork is the first thing that happens in that process!), and a non-zero process identifier in the original process. Or in other words, “not pid” is true only if we’re in the new process.

fork and wait are not available on Windows, but you can use the spawn function instead. Unfortunately, there’s no standard version of spawn that searches for an executable along the path, so you have to do that yourself.

Using the os module to run another program (Windows)

import os
import string

def run(program, *args):
    # find executable
    for path in string.split(os.environ["PATH"], os.pathsep):
        file = os.path.join(path, program) + ".exe"
        try:
            return os.spawnv(os.P_WAIT, file, (file,) + args)
        except os.error:
            pass
    raise os.error, "cannot find executable"

run("python", "hello.py")

print "goodbye"

You can also use spawn to run other programs in the background. The following example adds an optional mode argument to the run function; when set to os.P_NOWAIT, the script doesn’t wait for the other program to finish.

The default flag value os.P_WAIT tells spawn to wait until the new process is finished. Other flags include os.P_OVERLAY which makes spawn behave like exec, and os.P_DETACH which runs the new process in the background, detached from both console and keyboard.

Using the os module to exit the current process

import os
import sys

try:
    sys.exit(1)
except SystemExit, value:
    print "caught exit(%s)" % value

try:
    os._exit(2)
except SystemExit, value:
    print "caught exit(%s)" % value

print "bye!"
Output: caught exit(1)

Related:

Python Basics
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range