← Back to Python Course | Chapter 12: Standard Library | Lesson 1 of 9

Python os मॉड्यूल

os मॉड्यूल आपके program को computer की files और folders से बात करने देता है, जैसे file cabinet के लिए एक remote control। आप चीज़ें list, create और remove कर सकते हैं।
Syntax
python
import os

os.getcwd()
os.listdir(path)
os.path.join(part1, part2)

Working Directory प्राप्त करना

os मॉड्यूल आपकी script और underlying operating system के बीच मुख्य interface है।

os.getcwd() उस directory का absolute path लौटाता है जहाँ से आपका program शुरू हुआ था या जिसमें बाद में बदल गया, जो अक्सर बाकी relative paths बनाने के लिए reference point होता है।

उदाहरण: Getting Working Directory

python
import os
print(os.getcwd())

Folders बनाना और मिटाना

os.mkdir() एक नई directory बनाता है और अगर path में कोई parent directory पहले से मौजूद नहीं है तो fail हो जाता है, जबकि os.makedirs() ज़रूरत की पूरी chain की intermediate directories बना देता है।

os.rmdir() किसी directory को तभी हटाता है जब वो पूरी तरह खाली हो, और अगर उसमें अब भी files हों तो delete करने से मना कर देता है।

उदाहरण: Creating and Deleting Folders

python
import os

os.makedirs("demo_dir/sub", exist_ok=True)  # creates every intermediate directory needed
os.rmdir("demo_dir/sub")  # only works because the directory is empty
print("Folder created and removed")

Directory की सामग्री List करना

os.listdir(path) दी गई folder के अंदर मौजूद हर file और subdirectory का नाम एक साधारण strings की list के रूप में लौटाता है, बिना दोनों तरह की entries में फ़र्क़ किए।

आप आमतौर पर इसके बाद os.path.isfile() या os.path.isdir() का इस्तेमाल करते हैं अगर आपको files और folders में अंतर करना हो।

उदाहरण: Listing Directory Contents

python
import os
print(os.listdir("."))

Paths को Join और Split करना

साधारण string concatenation से file paths बनाना अलग-अलग operating systems पर टूट जाता है, क्योंकि Windows backslashes इस्तेमाल करता है जबकि Unix-जैसे systems forward slashes।

os.path.join() उस platform के लिए सही separator डाल देता है जिस पर code चल रहा है, इसलिए एक ही code हर जगह valid paths बनाता है।

उदाहरण: Joining and Splitting Paths

python
import os
path = os.path.join("folder", "file.txt")  # inserts the correct separator for the current OS
print(path)

File का अस्तित्व Check करना

os.path.exists() यह जाँचता है कि दिए गए path पर कुछ भी -- file या directory -- मौजूद है या नहीं, और अगर नहीं है तो error की बजाय False लौटाता है।

os.path.isfile() और os.path.isdir() इस check को और सटीक बनाते हैं, जिससे आप यह भी पक्का कर सकते हैं कि path किस तरह की चीज़ है।

उदाहरण: Checking File Existence

python
import os
print(os.path.exists("."))  # True whether it's a file or a directory
print(os.path.isdir("."))  # confirms it's specifically a directory
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.