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 understand and apply file system operations across platforms.
1. Overview of os module
Python’s os module provides common operating system functionalities.
2. Purpose of os module
It allows handling files and directories, making programs platform‑independent.
3. Common methods
1. os.name
Returns a string indicating the platform (e.g., 'nt' for Windows, 'posix' for Unix).
import os
print(os.name)On Windows this outputs 'nt'.
2. os.getcwd()
Gets the current working directory.
import os
print(os.getcwd())Result shown in the image.
3. os.listdir()
Returns a list of files and directories in the specified path.
import os
name = os.listdir(os.getcwd())
print(name)Result shown in the image.
4. os.remove()
Deletes a file, e.g., removing a file named 2.txt.
import os
os.remove("2.txt")5. os.system()
Executes a shell command.
import os
name = os.system('dir')
print(name)Result shown in the image.
6. os.sep
Provides the OS‑specific path separator.
import os
print(os.sep) # Windows returns '\\'7. os.linesep
String giving the line terminator used by the platform.
print(os.linesep) # '
' on Windows, '
' on Linux, '\r' on macOS8. os.path.split()
Returns a tuple (directory, filename) for a given path.
os.path.split('C:\\Python25\\abc.txt')9. os.path.isfile() and os.path.isdir()
Check whether a path is a file or a directory.
os.path.isdir(os.getcwd())
os.path.isfile('a.txt')10. os.path.exists()
Tests whether a given path exists.
os.path.exists('C:\\Python25\\abc.txt')
os.path.exists('C:\\Python25')11. os.path.abspath(name)
Gets the absolute path of a file.
import os
name = os.path.abspath("1.doc")
print(name)12. os.path.normpath(path)
Normalizes a pathname.
import os
name = os.path.normpath("1.doc")
print(name)13. os.path.getsize(name)
Returns the size of a file; returns 0 for directories.
import os
size = os.path.getsize("1.doc")
print(size)14. os.path.splitext()
Splits the filename from its extension.
os.path.splitext('a.txt')15. os.path.join(path, name)
Joins directory and file name into a full path.
os.path.join('c:\\Python', 'a.txt')
os.path.join('c:\\Python', 'f1')16. os.path.basename(path)
Returns the file name from a path.
os.path.basename('a.txt')
os.path.basename('c:\\Python\\a.txt')17. os.path.dirname(path)
Returns the directory component of a path.
os.path.dirname('c:\\Python\\a.txt')4. Summary
This article introduced the usage of Python’s os module, demonstrated essential file‑handling functions, and provided practical code examples to help readers understand and apply these operations in their programs.
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.
