The os
module in Python provides a way to interact with the operating system. It allows you to perform tasks such as navigating the file system, managing files and directories, and executing system commands. Below is a list of some commonly used functions in the os
module, along with their descriptions and links to detailed guides for each function.
Python os Module Functions Table
Function | Description |
---|---|
listdir() | Returns a list of the names of the entries in the directory. |
mkdir() | Creates a new directory. |
makedirs() | Creates a directory and all intermediate directories. |
remove() | Deletes a file. |
rmdir() | Deletes a directory. |
removedirs() | Deletes a directory and all empty intermediate directories. |
rename() | Renames a file or directory. |
replace() | Renames a file or directory, replacing the destination if it already exists. |
stat() | Performs a stat system call on the given path. |
chmod() | Changes the mode of path to the numeric mode. |
chown() | Changes the owner and group id of path. |
getcwd() | Returns the current working directory. |
chdir() | Changes the current working directory to the given path. |
path.join() | Joins one or more path components. |
path.exists() | Returns True if the path refers to an existing path or an open file descriptor. |
path.isfile() | Returns True if the path is an existing regular file. |
path.isdir() | Returns True if the path is an existing directory. |
path.getsize() | Returns the size, in bytes, of path. |
path.abspath() | Returns a normalized absolute version of the path. |
path.basename() | Returns the base name of pathname path. |
path.dirname() | Returns the directory name of pathname path. |
path.splitext() | Splits the pathname path into a pair (root, ext). |
path.expanduser() | Expands ~ or ~user in the given path to the user’s home directory. |
walk() | Generates the file names in a directory tree. |
system() | Executes the command in a subshell. |
popen() | Opens a pipe to or from command. |
environ | A mapping object representing the string environment. |
urandom() | Returns a string of n random bytes suitable for cryptographic use. |
access() | Uses the real uid/gid to test for access to path. |
dup() | Duplicates a file descriptor. |
dup2() | Duplicates file descriptor fd to fd2. |
fsync() | Forces a write of everything to disk. |
getpid() | Returns the current process id. |
getppid() | Returns the parent process id. |
getenv() | Returns the value of the environment variable key if it exists, or default if it doesn’t. |
putenv() | Sets the environment variable named key to the string value. |
setsid() | Creates a new session and sets the process group id. |
For more detailed information on each function, refer to the official Python documentation.