Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The os Module

The os module contains an interface to many operating system-specific functions to manipulate processes, files, file descriptors, directories and other “low level” features of the OS. Programs that import and use os stand a better chance of being portable between different platforms. Portable programs must depend only on functions that are supported for all platforms (e.g., unlink and opendir), and perform all pathname manipulation with os.path.

The os module exports the following variables that characterize your operating system.

os.name

A name for the operating system, for example 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce'. Note that Mac OS X has an os.name of 'posix'; but sys.platform is 'darwin'. Windows XP has an os.name of 'nt'.

os.curdir

String representing the current directory ('.', generally).

os.pardir

String representing the parent directory ('..', generally).

os.sep, os.altsep

The (or a most common) pathname separator ('/' or ':' or '\') and the alternate pathname separator (None or '/'). Most of the Python library routines will translate '/' to the correct value for the operating system (typically, '\' on Windows. It is best to always use os.path rather than these low-level constants.

os.pathsep

The component separator used in $PATH (':', generally).

os.linesep

The line separator in text files ('\n' or '\015\012'). This is already part of the readlines function.

os.defpath

The default search path for executables, for example, ':/bin:/usr/bin' or '.;C:\\bin'.

The os module has a large number of functions. Many of these are not directly related to file manipulation. However, a few are commonly used to create and remove files and directories. Beyond these basic manipulations, the shutil module supports a variety of file copy operations.

os.chdir path

Change the current working directory to the given path. This is the directory which the OS uses to transform a relative file name into an absolute file name.

os.getcwd

Returns the path to the current working directory. This is the directory which the OS use to transform a relative file name into an absolute file name.

os.listdir path

Returns a list of all entries in the given directory.

os.mkdir path [mode]

Create the given directory. In GU/Linux, the mode can be given to specify the permissions; usually this is an octal number. If not provided, the default of 0777 is used, after being updated by the OS umask value.

os.rename source destination

Rename the source filename to the destination filename. There are a number of errors that can occur if the source file doesn't exist or the destination file already exists or if the two paths are on different devices. Each OS handles the situations slightly differently.

os. remove( file )

Remove (also known as delete or unlink) the file. If you attempt to remove a directory, this will raise OSError. If the file is in use, the standard behavior is to remove the file when it is finally closed; Windows, however, will raise an exception.

os.rmdir path

Remove (also known as delete or unlink) the directory. if you attempt to remove an ordinary file, this will raise OSError.

Here's a short example showing some of the functions in the os module.

>>> 
import os

>>> 
os.chdir("/Users/slott")

>>> 
os.getcwd()

'/Users/slott'
>>> 
os.listdir(os.getcwd())

['.bash_history', '.bash_profile', '.CFUserTextEncoding', '.DS_Store', 
'.fonts.cache-1', '.idlerc', '.jedit', '.leoRecentFiles.txt', '.lpoptions', 
'.sqlite_history', '.ssh', '.subversion', '.Trash', '.viminfo', '.xxe', 
'argo.user.properties', 'Desktop', 'Documents', 'idletest', 'Library', 
'Movies', 'Music', 'Pictures', 'Public', 'Sites']

 
 
  Published under the terms of the Open Publication License Design by Interspire