Function overview
Function overview
The following is an incomplete list of the most commonly used functions and attributes from the modules we have used in this chapter. The list is not meant to be exhaustive, but rather to give you a quick overview of the most important functions and attributes. For a complete list, please refer to the Python documentation.
os
Concept: The os module
| Function / attribute | Description | Example |
|---|---|---|
| os.getcwd() | Returns the current working directory. | os.getcwd() β '/home/admin' |
| os.chdir(path) | Changes the current working directory. | os.chdir("/var/log") |
| os.listdir(path) | Returns a list with the names of the entries in a directory. | os.listdir("/etc") |
| os.walk(path) | Walks a directory tree recursively, yielding (dirpath, dirnames, filenames). | for root, dirs, files in os.walk("/var/log"): |
| os.mkdir(path) | Creates a single directory. | os.mkdir("backups") |
| os.makedirs(path, exist_ok=True) | Creates a directory and all missing parent directories. | os.makedirs("/tmp/a/b/c", exist_ok=True) |
| os.remove(path) | Deletes a file. | os.remove("old.log") |
| os.rmdir(path) | Deletes an empty directory. | os.rmdir("backups") |
| os.rename(src, dst) | Renames or moves a file or directory. | os.rename("app.log", "app.log.1") |
| os.chmod(path, mode) | Changes the permissions of a file (Unix-style octal mode). | os.chmod("script.py", 0o755) |
| os.stat(path) | Returns file metadata (size, owner, timestamps, permissions, ...). | os.stat("app.log").st_size |
| os.path.exists(path) | Checks whether a file or directory exists. | os.path.exists("/etc/hosts") β True |
| os.path.isfile(path) | Checks whether a path is an existing file. | os.path.isfile("/etc/hosts") β True |
| os.path.isdir(path) | Checks whether a path is an existing directory. | os.path.isdir("/etc") β True |
| os.path.abspath(path) | Converts a relative path to an absolute path. | os.path.abspath("script.py") β '/home/admin/script.py' |
| os.path.join(a, b, ...) | Joins path components using the correct separator for the OS. | os.path.join("/var", "log", "syslog") |
| os.path.basename(path) | Returns the last component of a path (the file name). | os.path.basename("/var/log/syslog") β 'syslog' |
| os.path.dirname(path) | Returns the directory part of a path. | os.path.dirname("/var/log/syslog") β '/var/log' |
| os.path.getsize(path) | Returns the size of a file in bytes. | os.path.getsize("app.log") β 20480 |
| os.environ | Dictionary-like object containing the environment variables. | os.environ["PATH"] |
| os.getenv(key, default=None) | Returns an environment variable, or the default if it doesn't exist. | os.getenv("HOME", "/tmp") |
| os.getpid() | Returns the process ID of the running script. | os.getpid() β 48213 |
| os.cpu_count() | Returns the number of logical CPUs. | os.cpu_count() β 8 |
| os.name | Name of the OS family: 'posix' (Linux/macOS) or 'nt' (Windows). | if os.name == "nt": |
| os.sep | The path separator of the OS: '/' or '\'. | os.sep β '/' |
| os.uname() | Returns system information (hostname, kernel release, architecture). Unix only. | os.uname().nodename β 'web01' |
| os.geteuid() | Returns the effective user ID; 0 means root. Unix only. | if os.geteuid() != 0: |
sys
Concept: The sys module
| Function / attribute | Description | Example |
|---|---|---|
| sys.argv | List of command-line arguments; sys.argv[0] is the script name. | ./backup.py /data β sys.argv[1] is '/data' |
| sys.exit(code) | Stops the script with an exit code (0 = success, anything else = error). | sys.exit(1) |
| sys.stdin | The standard input stream; useful for reading piped input. | for line in sys.stdin: |
| sys.stdout | The standard output stream. | sys.stdout.write("OK\n") |
| sys.stderr | The standard error stream; use it for error messages. | print("Error!", file=sys.stderr) |
| sys.platform | Identifies the platform: 'linux', 'win32', 'darwin', ... | if sys.platform == "linux": |
| sys.version | String with the version of the Python interpreter. | sys.version β '3.14.0 (main, ...)' |
| sys.version_info | Version as a tuple-like object; handy for checking a minimum version. | if sys.version_info < (3, 10): |
| sys.executable | Absolute path of the Python interpreter running the script. | sys.executable β '/usr/bin/python3' |
| sys.path | List of directories Python searches when importing modules. | print(sys.path) |
pathlib
Concept: The pathlib module
The pathlib module allows us to construct paths that work on both Windows and *nix variants (Linux, BSD, macOS) and never include an extra or missing slash.
Paths are concatenated using the / operator which in this case has nothing to do with division.
from pathlib import Path
log_file = Path("/var/log") / "nginx" / "access.log"
print(log_file) # Output: /var/log/nginx/access.logFiles
Concept: Creating, moving and deleting files and directories
The shutil module provides a high-level interface for file operations. It can be used to copy, move and delete files and directories as well as to create archives (but the zipfile and tarfile modules are preferred).
| Function | Description | Example |
|---|---|---|
| shutil.copy(src, dst) | Copies a file from src to dst. | shutil.copy("file.txt", "backup/file.txt") |
| shutil.copy2(src, dst) | Copies a file from src to dst and preserve as much metadata as possible. | shutil.copy2("file.txt", "backup/file.txt") |
| shutil.copytree(src, dst) | Copies a directory and it's contents from src to dst. | shutil.copytree("dir", "backup/dir") |
| shutil.move(src, dst) | Recursively moves a file or directory from src to dst. | shutil.move("file.txt", "backup/file.txt") |
| shutil.rmtree(path) | Recursively deletes a directory and it's contents. | shutil.rmtree("dir") |
| shutil.which(cmd) | Returns the path of an executable or None if not found. | shutil.which("python") |
| shutil.chown(path, user=None, group=None) | Changes the owner and/or group of a file or directory for a file on a *nix filesystem. | shutil.chown("file.txt", user="sebastiaan") |
The shutil module has two important problem, it cannot be used to delete a single file, only directories and it cannot be used to create directories. For that the pathlib module can be used.
| Function | Description | Example |
|---|---|---|
| Path.unlink() | Deletes a file. | Path("file.txt").unlink() |
| Path.rmdir() | Deletes an empty directory. | Path("dir").rmdir() |
| Path.mkdir(path, parents=False, exist_ok) | Creates a single directory (including non-existing parents when parents=True) | Path("dir").mkdir() |
Subprocess
Concept: subprocess
The subprocess module allows us to run other programs from our script.
The run function is the simplest way to run a command. The functions has three important parameters:
- A list of strings that make up the command and its arguments. The first string is the command, the rest are arguments.
capture_output(default:False) which determines whether the output of the command is printed to the terminal or captured in the result object.text(default:False) which determines whether the output is returned as bytes or as a string.
import subprocess
result = subprocess.run(["ping", "-c", "1", "1.1.1.1"])The result object has a few useful attributes:
| Attribute | Description |
|---|---|
| stdout | The output of the command, as bytes or string depending on the text parameter. |
| stderr | The error output of the command, as bytes or string depending on the text parameter. |
| returncode | The exit code of the command. 0 means success, anything else is an error. |