Next / Previous / Contents / TCC Help System / NM Tech homepage

28.7. os: The operating system interface

The variables and methods in the os module allow you to interact with files and directories. In most cases the names and functionalities are the same as the equivalent C language functions, so refer to Kernighan and Ritchie, The C Programming Language, second edition, or the equivalent for more details.


Change the current working directory to that given by string p


Change the permissions for pathname p to m. See module stat, below, for symbolic constants to be used in making up m values.


Change the owner of pathname p to user id u and group id g.


A dictionary whose keys are the names of all currently defined environmental variables, and whose values are the values of those variables.


The exception raised for errors in this module.


Replace the current process with a new process executing the file at pathname p, where A is a list of the strings to be passed to the new process as command line arguments.


Like execv(), but you supply a dictionary E that defines the environmental variables for the new process.


Exit the current process and return status code n. This method should be used only by the child process after a fork(); normally you should use sys.exit().


Fork a child process. In the child process, this function returns 0; in the parent, it returns the child's process ID.


Returns the current working directory name as a string.


Returns the effective group ID.


Returns the effective user ID.


Returns the current process's group ID. To decode user IDs, see the grp standard module.


Returns the current process's process ID.


Returns the parent process's PID (process ID).


Returns the current process's user ID. To decode user IDs, see the pwd standard module.


Send signal s to the process whose process ID is p.


Create a hard link to s and call the link d.


Return a list of the names of the files in the directory whose pathname is p. This list will never contain the special entries '.' and '..' for the current and parent directories. The entries may not be in any particular order.


Like stat(), but if p is a link, you will get the status tuple for the link itself, rather than the file it points at.

makedirs(p[, mode])

Works like mkdir(), but will also create any intermediate directories between existing directories and the desired new directory.


Create a directory at pathname p. You may optionally specify permissions m; see module stat below for the interpretation of permission values.


Create a named pipe with name p and open mode m. The server side of the pipe will open it for reading, and the client side for writing. This function does not actually open the fifo, it just creates the rendezvous point.


Renice (change the priority) of the current process by adding i to its current priority.


If p is the pathname to a soft (symbolic) link, this function returns the pathname to which that link points.


Removes the file with pathname p, as in the Unix rm command. Raises OSError if it fails.


Similar to remove(), but also removes any other parent directory in the path that has no other children.

rename(po, pn)

Rename path po to pn.


Remove the directory at path p.


Return a status tuple describing the file or directory at pathname p. See module stat, below, for the interpretation of a status tuple. If p is a link, you will get the status tuple of the file to which p is linked.


Create a symbolic link to path s, and call the link d.


Execute the command in string c as a sub-shell. Returns the exit status of the process.


Returns a tuple of statistics about the current process's elapsed time. This tuple has the form (u,s,u',s',r) where u is user time, s is system time, u' and s' are user and system time including all child processes, and r is elapsed real time. All values are in seconds as floats.


Returns a new, open temporary file, with update mode "w+b". This file will not appear in any directory, and will disappear when it is no longer in use.


Sets the “umask” that determines the default permissions for newly created files. Returns the previous value. Each bit set in the umask corresponds to a permission that is not granted by default.


Returns a tuple of strings descriping the operating system's version: (s,n,r,v,m) where s is the name of the operating system, n is the name of the processor (node) where you are running, r is the operating system's version number, v is the major version, and m describes the type of processor.


Return a string of n random bytes. These bytes should be sufficiently random for use in cryptographic applications.


The t argument must be a tuple (a, m) where a and m are epoch times. For pathname p, set the last access time to a and the last modification to m.


Wait for the termination of a child process. Returns a tuple (p,e) where p is the child's process ID and e is its exit status.


Like wait(), but it waits for the process whose ID is p. The option value o specifies what to do if the child is still running. If o is 0, you wait for the child to terminate. Use a value of os.WNOHANG if you don't want to wait.


See waitpid() above.