You are currently viewing Python ‘os’ Module Cheat Sheet

Python ‘os’ Module Cheat Sheet

File and Directory Operations

Get Current Working Directory

current_directory = os.getcwd()

Change Directory

os.chdir("/path/to/directory")

List Files and Directories in a Directory

files_and_directories = os.listdir("/path/to/directory")

Check if a Path Exists

path_exists = os.path.exists("/path/to/file_or_directory")

Check if a Directory Exists

directory_exists = os.path.isdir("/path/to/directory")

Check if a File Exists

file_exists = os.path.isfile("/path/to/file")

Create a Directory

os.mkdir("/path/to/new_directory")

Create Intermediate Directories if They Don’t Exist

os.makedirs("/path/to/nested/directory", exist_ok=True)

Remove a File

os.remove("/path/to/file")

Remove an Empty Directory

os.rmdir("/path/to/empty_directory")

Remove a Directory and Its Contents

os.removedirs("/path/to/directory_to_remove")

File and Path Manipulation

Join Paths

path = os.path.join("/path", "to", "file_or_directory")

Get the Absolute Path

absolute_path = os.path.abspath("/path/to/file_or_directory")

Get the Base Name of a Path

base_name = os.path.basename("/path/to/file_or_directory")

Get the Directory Name of a Path

base_name = os.path.basename("/path/to/file_or_directory")

Split a Path into a Tuple (head, tail)

head, tail = os.path.split("/path/to/file_or_directory")

File Information

Get Size of a File

file_size = os.path.getsize("/path/to/file")

Get Modification Time of a File

modification_time = os.path.getmtime("/path/to/file")

Get Creation Time of a File

creation_time = os.path.getctime("/path/to/file")

Convert Modification Time to a Readable Format

modification_time = os.path.getmtime("/path/to/file")
formatted_time = time.ctime(modification_time)

Environmental Variables

Get the Value of an Environment Variable

value = os.getenv("MY_VARIABLE", default_value)

Set an Environment Variable

os.environ["MY_VARIABLE"] = "value"

Removing an Environment Variable

os.environ.pop("MY_VARIABLE", None)

Miscellaneous

Execute a Command in the Shell

os.system("command_to_execute")

Check if the Operating System is Windows

is_windows = os.name == 'nt'

Check if the Operating System is POSIX (Linux, macOS)

is_posix = os.name == 'posix'

Table of Commands

File and Directory Operations

CommandDescription
os.getcwd()Get Current Working Directory
os.chdir("/path/to/directory")Change Directory
os.listdir("/path/to/directory")List Files and Directories in a Directory
os.path.exists("/path/to/file_or_directory")Check if a Path Exists
os.path.isdir("/path/to/directory")Check if a Directory Exists
os.path.isfile("/path/to/file")Check if a File Exists
os.mkdir("/path/to/new_directory")Create a Directory
os.makedirs("/path/to/nested/directory", exist_ok=True)Create Intermediate Directories if They Don’t Exist
os.remove("/path/to/file")Remove a File
os.rmdir("/path/to/empty_directory")Remove an Empty Directory
os.removedirs("/path/to/directory_to_remove")Remove a Directory and Its Contents

File and Path Manipulation

CommandDescription
os.path.join("/path", "to", "file_or_directory")Join Paths
os.path.abspath("/path/to/file_or_directory")Get the Absolute Path
os.path.basename("/path/to/file_or_directory")Get the Base Name of a Path
os.path.dirname("/path/to/file_or_directory")Get the Directory Name of a Path
os.path.split("/path/to/file_or_directory")Split a Path into a Tuple (head, tail)

File Information

CommandDescription
os.path.getsize("/path/to/file")Get Size of a File
os.path.getmtime("/path/to/file")Get Modification Time of a File
os.path.getctime("/path/to/file")Get Creation Time of a File
time.ctime(modification_time)Convert Modification Time to a Readable Format

Environmental Variables

CommandDescription
os.getenv("MY_VARIABLE", default_value)Get the Value of an Environment Variable
os.environ["MY_VARIABLE"] = "value"Set an Environment Variable
os.environ.pop("MY_VARIABLE", None)Removing an Environment Variable

Miscellaneous

CommandDescription
os.system("command_to_execute")Execute a Command in the Shell
os.name == 'nt'Check if the Operating System is Windows
os.name == 'posix'Check if the Operating System is POSIX (Linux, macOS)

By using these commands from the os module, you can improve your file and system operations in Python, making your code more robust and platform-independent. This cheat sheet serves as a quick reference for your the needs in the os module.

Leave a Reply