Master Python’s os Module: Essential File & Directory Operations Explained
This article provides a comprehensive guide to Python’s os module, covering its purpose, key functions such as os.name, os.getcwd, os.listdir, and various path utilities, complete with code examples and output screenshots to help readers effectively manage files and directories across platforms.
1. Overview of the os Module
Python’s os module provides a portable way of using operating system dependent functionality, including file and directory manipulation.
2. Why Use the os Module
It enables programs to work across different platforms without changing code, making it essential for handling files and directories in a platform‑independent manner.
3. Common Functions
1. os.name
Returns a string indicating the operating system type (e.g., "nt" for Windows, "posix" for Unix/Linux).
import os
print(os.name)On a Windows system this prints nt.
2. os.getcwd()
Returns the current working directory.
import os
print(os.getcwd())Result:
3. os.listdir()
Lists all files and directories in the specified path.
import os
name = os.listdir(os.getcwd())
print(name)Result:
4. os.remove()
Deletes a specified file.
import os
os.remove("2.txt")5. os.system()
Executes a shell command.
import os
name = os.system('dir')
print(name)Result:
6. Path Utilities
os.sep– platform‑specific path separator.
import os
print(os.sep) # Windows prints '\' os.linesep– line terminator for the current platform.
print(os.linesep) # '
' on Windows os.path.split()– splits a pathname into directory and file name.
os.path.split('C:\\Python25\\abc.txt')
# Returns ('C:\\Python25', 'abc.txt') os.path.isfile()and os.path.isdir() – check if a path is a file or directory.
os.path.isdir(os.getcwd()) # True if current path is a directory
os.path.isfile('a.txt') # False if file does not exist os.path.exists()– verifies whether a path exists.
os.path.exists('C:\\Python25\\abc.txt') # False if not present
os.path.exists('C:\\Python25') # True if directory exists os.path.abspath(name)– returns the absolute path of name.
import os
name = os.path.abspath('1.doc')
print(name) os.path.normpath(path)– normalizes a pathname.
import os
name = os.path.normpath('1.doc')
print(name) os.path.getsize(name)– returns file size (0 for directories).
import os
size = os.path.getsize('1.doc')
print(size)os.path.splitext()– splits filename and extension.
os.path.splitext('a.txt') # Returns ('a', '.txt') os.path.join(path, name)– joins directory and file name.
os.path.join('c:\\Python', 'a.txt') # 'c:\\Python\\a.txt'
os.path.join('c:\\Python', 'f1') # 'c:\\Python\\f1' os.path.basename(path)– returns the file name.
os.path.basename('a.txt') # 'a.txt'
os.path.basename('c:\\Python\\a.txt') # 'a.txt' os.path.dirname(path)– returns the directory component.
os.path.dirname('c:\\Python\\a.txt') # 'c:\\Python'4. Summary
This guide introduced the essential functions of Python’s os module for file and directory manipulation, demonstrated each function with clear code snippets and output images, and highlighted practical considerations for cross‑platform development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
