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.

chdir(p)

Change the current working directory to that given by string p

chmod(p,m)

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

chown(p,u,g)

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

environ

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

error

The exception raised for errors in this module.

execv(p,A)

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.

execve(p,A,E)

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

_exit(n)

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()

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

getcwd()

Returns the current working directory name as a string.

getegid()

Returns the effective group ID.

geteuid()

Returns the effective user ID.

getgid()

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

getpid()

Returns the current process's process ID.

getppid()

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

getuid()

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

kill(p,s)

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

link(s,d)

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

listdir(p)

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.

lstat(p)

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.

mkdir(p[,m])

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

mkfifo(p,m)

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.

nice(i)

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

readlink(p)

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

remove(p)

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

removedirs(p)

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.

rmdir(p)

Remove the directory at path p.

stat(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.

symlink(s,d)

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

system(c)

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

times()

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.

tmpfile()

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.

umask(m)

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.

uname()

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.

urandom(n)

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

utime(p,t)

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()

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.

waitpid(p,o)

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.

WNOHANG

See waitpid() above.