Standard Modules in Python
What is the os module in Python?
The os module in Python provides a way to interact with the operating system, offering functions to work with files, directories, and processes. It allows tasks like creating, deleting, and navigating directories, as well as handling file paths. It is useful for log file management by deleting or archiving old logs, preventing storage overload.
Section 1: File and Directory Operations
OS Modules Examples
# os.mkdir(path) - Creates a directory
os.mkdir("new_folder")
# os.system(command) - Executes a system command
os.system("echo Hello World") # Output: Hello World
# os.getcwd() - Returns the current working directory
print(os.getcwd()) # Prints the current working directory
# os.chdir(path) - Changes the current working directory
os.chdir("/path/to/directory")
# os.remove(path) - Removes a file
os.remove("file_to_delete.txt")
# os.rmdir(path) - Removes an empty directory
os.rmdir("empty_folder")
Section 2: Path Operations and File Checks
OS Modules Examples
# os.path.abspath(path) - Returns the absolute path of the specified file
print(os.path.abspath("file.txt")) # Prints the absolute path of 'file.txt'
# os.path.join(path, *paths) - Joins one or more path components
full_path = os.path.join("folder", "subfolder", "file.txt")
# os.path.basename(path) - Returns the file name from a path
print(os.path.basename("/path/to/file.txt")) # Output: 'file.txt'
# os.path.dirname(path) - Returns the directory name from a path
dir_name = os.path.dirname("/path/to/file.txt") # Output: '/path/to'
# os.path.exists(path) - Checks if a path exists
print(os.path.exists("file.txt")) # Output: True / False
# os.path.samefile(path1, path2) - Checks if two paths refer to the same file
same_path = os.path.samefile("file1.txt", "file2.txt") # Output: True / False
# os.path.split(path) - Splits the path into head (directory) and tail (file)
path_splited = os.path.split("/path/to/file.txt")
print(path_splited) # Output : ['/path/to', 'file.txt']
# os.path.split(path)[0] - Gets the directory part of the path
head = os.path.split("/path/to/file.txt")[0] # '/path/to'
# os.path.split(path)[1] - Gets the file name part of the path
tail = os.path.split("/path/to/file.txt")[1] # 'file.txt'
Section 3: Path Validation Methods
OS Modules Examples
# os.path.isabs(path) - Checks if the path is absolute
print(os.path.isabs("/path/to/file.txt"))
# os.path.exists(path) - Checks if the path exists
print(os.path.exists("/path/to/file_or_directory"))
# os.path.isdir(path) - Checks if the path is a directory
print(os.path.isdir("/path/to/directory"))
# os.path.isfile(path) - Checks if the path is a file
print(os.path.isfile("/path/to/file.txt"))
# os.path.getsize(path) - Returns the size of the file in bytes
print(os.path.getsize("/path/to/file.txt"))
# os.path.samefile(path1, path2) - Checks if two paths refer to the same file
print(os.path.samefile("/path/to/file1.txt", "/path/to/file2.txt"))
Exercise
- Top 5 Python Problems on Standard Modules - os, random, datetime & time:
- - Write a Python script to create a directory named test_folder and verify its existence.
- - Write a script to list all files in a given directory and print their sizes in bytes.
- -
- -
- -