1. Home
  2. Computing & Technology
  3. Python

Process Manipulation With Python's os Module

From , former About.com Guide

1 of 4

General Purpose Variables

Python's os module provides a cross-platform interface to many system-level services. In effect, the os module usually offers a subset of posix without the overhead the superset requires. On Windows systems, it will likely import 'nt'. Mac OS uses 'mac'. You can tell which is used by invoking 'os.name' as described below.

Being familiar with some of the more common functions and variables available through the os module is therefore important for more advanced Python programming.

Four general-purpose variables are defined by os:

  • os.environ: Maps current Python environmental variables for access by Python. These variables represent Python's knowledge of its environment. Therefore, they can be changed without effecting the greater runtime environment. For a list of all variables, run the following from a Python shell:
    for k in os.environ: print "%s: %s" %(k, os.environ[k])
  • os.linesep: The string used to indicate line breaks. On Unix-based systems (Unix, Linux, Mac OS X), this is '\n'. However, if you want to emulate a Windows system, you can change this to '\r\n' to represent the carriage return and newline feed strings used on Windows platforms.
  • os.name: The name of the operating system-dependent module imported for OS-related access. Options are: posix, nt, dos, mac, ce, java, os2, or riscos.
  • os.path: The OS-dependent module for operations related to filesystem paths. Invoking this without arguments will give you the system path to the OS-dependent module being used. For example, on a Linux system, 'os.path' returns:
    <module 'posixpath' from '/usr/lib/python2.5/posixpath.pyc'>
    .
Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Python Library
  5. Python Programming - Python Library - Python's os Module - Process Manipulation With Python's os Module

©2009 About.com, a part of The New York Times Company.

All rights reserved.