import-os
Table of Contents
import os
import os is a Python import that provides functions for interacting with the operating system: file paths, environment variables, process information, and filesystem operations. Use this for cross-platform file handling, reading environment, and running system commands.
Example
# Python # description: list files in current directory, get home path import os # Print current working directory print(os.getcwd()) # List files in directory for name in os.listdir('.'): print(name) # Access environment variables home = os.environ.get('HOME') print(f"Home: {home}") # Get process ID print(f"Process ID: {os.getpid()}")
Common functions
os.getcwd(): current working directoryos.chdir(path): change directoryos.listdir(path): list directory contentsos.makedirs(path): create directoriesos.remove(path): delete a fileos.rmdir(path): delete an empty directoryos.rename(src, dst): rename fileos.path.exists(path): check if path existsos.path.join(...): join path components (usepathlibfor modern code)os.environ: environment variables dictionaryos.getenv(key, default): get environment variableos.getpid(): current process IDos.walk(path): walk directory tree recursively
import-os.md · Last modified: by 127.0.0.1
