Python os Module in Hindi: Files, Folders और Directories Manage करें
Python os Module क्या है? Files, Folders और Directories Manage करना
Python में कई बार हमें केवल code लिखना ही नहीं, बल्कि computer पर मौजूद files और folders के साथ भी काम करना पड़ता है। उदाहरण के लिए current folder का पता लगाना, किसी directory की files देखना, नया folder बनाना, file का नाम बदलना या किसी file को delete करना। इन कामों के लिए Python का built-in os module उपयोगी है।
इस tutorial में आप सीखेंगे कि import os कैसे काम करता है और os.getcwd(), os.chdir(), os.listdir(), os.mkdir(), os.makedirs(), os.path.exists(), os.rename(), os.remove() और दूसरे जरूरी functions कैसे इस्तेमाल किए जाते हैं। सभी examples beginner-friendly रखे गए हैं।
अगर आप Python में files के साथ काम करना शुरू कर रहे हैं, तो पहले Python File Handling in Hindi: Core Concepts & Workflow, Python File Reading in Hindi और Python File Writing in Hindi lessons भी पढ़ें।
os module के selected file, folder और directory operations पर focus है। shutil, pathlib, sys, processes और environment variables को detail में नहीं पढ़ाया गया है।
Python os Module क्या है?
os Python का built-in module है, जो operating system से संबंधित कई कामों के लिए functions देता है। इससे आप directories के साथ काम कर सकते हैं, files और folders की जानकारी ले सकते हैं, directory बना सकते हैं और file या folder का नाम बदल सकते हैं।
यह code current working directory का path print करता है। आपके computer पर output अलग हो सकता है।
os Module की जरूरत क्यों पड़ती है?
मान लीजिए आपके पास ऐसा Python project है जिसमें reports को अलग-अलग folders में रखना है। हर बार manually folders बनाना या file locations check करना मुश्किल हो सकता है। os module से इन tasks को Python code के जरिए automate किया जा सकता है।
- Current working directory पता करना।
- Directory बदलना।
- Files और folders की list देखना।
- नया folder या nested folders बनाना।
- File या folder मौजूद है या नहीं, check करना।
- File या folder rename करना।
- File या खाली folder delete करना।
- Cross-platform तरीके से file paths बनाना।
os.remove() और os.rmdir() इस्तेमाल करने से पहले path को ध्यान से जाँचें।import os क्या करता है?
Python में module के functions इस्तेमाल करने से पहले उसे import करना पड़ता है। import os लिखने के बाद आप os.getcwd() जैसे functions call कर सकते हैं।
Possible Output:
यह केवल example output है। आपके system में path अलग होगा।
os.getcwd() — Current Working Directory
os.getcwd() का पूरा नाम current working directory है। यह बताता है कि Python program इस समय किस directory को अपनी working location मानकर चल रहा है। Relative file paths अक्सर इसी directory के आधार पर resolve होते हैं।
os.getcwd() से इसे check करें।os.chdir() — Directory बदलना
os.chdir() current working directory बदलता है। इसे उस directory के path के साथ call किया जाता है जहाँ आप आगे काम करना चाहते हैं। Directory मौजूद न हो या access न मिले, तो error आ सकती है।
ऊपर os.chdir() की line comment है, ताकि किसी गलत या non-existent path पर अनजाने में error न आए। अपने computer पर सही path देकर # हटाएँ।
r"C:\Users\YourName\Documents" इस्तेमाल कर सकते हैं। अपने computer का वास्तविक username और folder path डालें।os.listdir() — Files और Folders की List
os.listdir() किसी directory में मौजूद entries के नामों की list देता है। Path न देने पर यह current working directory की list देता है। इसमें files और subfolders दोनों के नाम आ सकते हैं।
Possible Output:
Output आपके folder की वास्तविक files और folders पर निर्भर करेगा।
किसी खास directory की list देखनी हो, तो उसका path दें:
os.mkdir() — नया Folder बनाना
os.mkdir() एक नया directory/folder बनाता है। जिस parent directory में folder बनाना है, वह पहले से मौजूद होनी चाहिए। अगर उसी नाम का folder पहले से मौजूद है, तो सामान्यतः FileExistsError आ सकती है।
Program की working directory में PythonPractice नाम का folder बनेगा। इस code को दोबारा चलाने से पहले देखें कि folder पहले से मौजूद तो नहीं है।
os.makedirs() — Nested Folders बनाना
जब एक साथ कई levels के folders बनाने हों, तो os.makedirs() इस्तेमाल कर सकते हैं। यह जरूरत पड़ने पर बीच के parent folders भी बना सकता है।
यह Projects, उसके अंदर Python और उसके अंदर Files directory बनाता है। exist_ok=True होने पर यदि पूरा target directory पहले से मौजूद है, तो केवल उसी कारण से error नहीं आती।
os.path.exists() — File या Folder Check करना
os.path.exists(path) check करता है कि दिए गए path पर कोई file या directory मौजूद है या नहीं। मौजूद होने पर True, अन्यथा False मिलता है।
इस check से आप file या folder बनाने से पहले पता लगा सकते हैं कि path पहले से मौजूद है या नहीं। लेकिन ध्यान रखें कि file system बदल सकता है; महत्वपूर्ण operations में errors को भी उचित तरीके से handle करें।
os.path.isfile() और os.path.isdir()
दोनों functions path के type को check करने में मदद करते हैं:
os.path.isfile(path)— path किसी मौजूद regular file को refer करता है या नहीं।os.path.isdir(path)— path किसी मौजूद directory/folder को refer करता है या नहीं।
os.rename() — File या Folder Rename करना
os.rename(old, new) किसी file या directory का नाम बदलने के लिए इस्तेमाल होता है। पुराने path का मौजूद होना जरूरी है, और नए path की स्थिति तथा permissions के अनुसार operation सफल या असफल हो सकता है।
यह example तभी rename करेगा जब old_notes.txt current working directory में मौजूद हो।
os.rmdir() — Empty Folder Delete करना
os.rmdir() किसी खाली directory को हटाता है। यदि folder में files या subfolders मौजूद हैं, तो यह सामान्यतः OSError देगा। यह function folder के अंदर की सामग्री को recursively delete नहीं करता।
os.remove() — File Delete करना
os.remove() किसी file को delete करता है। यह directory को delete करने के लिए नहीं है। Delete करने के बाद file वापस पाना आसान न भी हो, इसलिए पहले path और file name को ध्यान से check करें।
ऊपर का code वास्तविक file delete कर सकता है। इसे केवल अपनी test file और practice folder में चलाएँ। किसी महत्वपूर्ण file या folder पर delete operation करने से पहले backup रखें।
os.path.join() — File Paths बनाना
os.path.join() अलग-अलग path parts को सही separator के साथ जोड़ने के लिए इस्तेमाल होता है। यह hard-coded slash या backslash लिखने से बेहतर है, क्योंकि path separator operating system के अनुसार अलग हो सकता है।
Possible Output on Windows:
दूसरे operating systems पर separator अलग हो सकता है।
इसका उपयोग directory check, file open या file rename जैसे operations में भी कर सकते हैं:
Real-world File और Folder Management Example
अब एक छोटा example बनाते हैं जो project folder तैयार करेगा, उसमें notes folder बनाएगा, text file create करेगा और फिर path check करेगा। यह example केवल अपनी practice directory में चलाएँ।
Possible Output:
Output में path separator और directory listing आपके operating system के अनुसार अलग हो सकती है।
इस example में os.makedirs() directory structure बनाता है, os.path.join() file path तैयार करता है, open() और write() text लिखते हैं और os.path.isfile() file के मौजूद होने की जाँच करता है।
Common Mistakes
यदि folder मौजूद नहीं है या path गलत है, तो FileNotFoundError आ सकती है।
os.getcwd() से current directory check करें और os.chdir() को सही, मौजूद path दें।
अगर folder पहले से मौजूद है, तो FileExistsError आ सकती है।
Nested directories के लिए os.makedirs("Projects/Python", exist_ok=True) उपयोगी है।
os.rmdir() केवल खाली directory हटाता है। Folder के अंदर files हों, तो operation fail हो सकता है।
इस lesson में केवल empty folder deletion शामिल है। Recursive folder deletion के लिए दूसरे tools और अतिरिक्त सावधानी की जरूरत होती है।
os.remove() files के लिए है, directories के लिए नहीं।
File के लिए os.remove() और खाली directory के लिए os.rmdir() इस्तेमाल करें। Delete करने से पहले target path को verify करें।
Windows और Unix-like operating systems में path separators अलग हो सकते हैं।
Quick Exercise 🧠
किस function से current working directory का path मिलता है?
os.listdir()os.getcwd()os.mkdir()सही उत्तर: B — os.getcwd()
Final Coding Challenge
अब VS Code में os_practice.py नाम की file बनाएँ और नीचे दिए गए tasks पूरा करें। Delete और rename वाले tasks के लिए केवल अपनी बनाई हुई practice files का इस्तेमाल करें।
import osकरके current working directory print करें।os.listdir()से current directory की entries print करें।os.mkdir()सेMyOSPracticeनाम का folder बनाएँ; दोबारा run करने से पहले existence check करें।os.makedirs()सेMyOSPractice/Files/Notesnested structure बनाएँ औरexist_ok=Trueआजमाएँ।os.path.join()सेnotes.txtका path बनाएँ।os.path.exists(),os.path.isfile()औरos.path.isdir()का इस्तेमाल करके paths check करें।- Practice folder के अंदर एक text file बनाएँ और उसका नाम
os.rename()से बदलें। - एक खाली folder बनाएँ और
os.rmdir()से उसे हटाएँ। - एक temporary practice file बनाएँ, verify करें और फिर
os.remove()से delete करें। - Bonus: हर operation के बाद एक छोटा status message print करें और गलत path के कारण आने वाली error को समझें।
Quick Revision
| Function | काम |
|---|---|
os.getcwd() | Current working directory बताता है। |
os.chdir(path) | Current working directory बदलता है। |
os.listdir(path) | Directory की entries की list देता है। |
os.mkdir(path) | एक directory बनाता है। |
os.makedirs(path, exist_ok=True) | Nested directories बना सकता है। |
os.path.exists(path) | Path मौजूद है या नहीं check करता है। |
os.path.isfile(path) | Path regular file है या नहीं check करता है। |
os.path.isdir(path) | Path directory है या नहीं check करता है। |
os.rename(old, new) | File या directory rename करता है। |
os.rmdir(path) | खाली directory delete करता है। |
os.remove(path) | File delete करता है। |
os.path.join(a, b) | Path parts को सही separator के साथ जोड़ता है। |
FAQ — Python os Module
1. Python में os module क्या है?
os Python का built-in module है जो operating system से संबंधित काम, जैसे directories देखना, folder बनाना और file paths manage करना आसान बनाता है।
2. क्या os module install करना पड़ता है?
नहीं। os Python की standard library में शामिल है। सामान्यतः केवल import os लिखना होता है।
3. os.getcwd() और os.chdir() में क्या फर्क है?
os.getcwd() current working directory का path देता है, जबकि os.chdir() current working directory बदलता है।
4. os.listdir() क्या return करता है?
यह चुनी हुई directory में मौजूद entries के नामों की list return करता है। इसमें files और directories दोनों शामिल हो सकते हैं।
5. os.mkdir() और os.makedirs() में क्या अंतर है?
os.mkdir() एक directory बनाता है, जबकि os.makedirs() जरूरत पड़ने पर nested parent directories भी बना सकता है।
6. File मौजूद है या नहीं, कैसे check करें?
os.path.exists(path) path मौजूद होने की जाँच करता है। विशेष रूप से regular file check करने के लिए os.path.isfile(path) इस्तेमाल करें।
7. os.rmdir() और os.remove() में क्या अंतर है?
os.rmdir() खाली directory हटाता है, जबकि os.remove() file हटाता है।
8. os.path.join() का उपयोग क्यों करें?
यह path parts को operating system के अनुसार सही separator के साथ जोड़ता है, जिससे hard-coded slash या backslash पर निर्भरता कम होती है।
9. क्या os module से files permanently delete हो सकती हैं?
os.remove() file को file system से delete करता है। उसे वापस पाना संभव हो भी सकता है और नहीं भी, इसलिए delete operation से पहले backup और path verification करें।
10. क्या os module Windows और Linux दोनों पर काम करता है?
हाँ, os module cross-platform है, लेकिन कुछ behaviors और paths operating system के अनुसार अलग हो सकते हैं। os.path.join() जैसे functions portable paths बनाने में मदद करते हैं।
Summary
Python os Module = Operating system से जुड़ी file, folder और directory operations को Python code से manage करना।
इस lesson में आपने os module import करना, current directory पता करना और बदलना, directory listing देखना, folders बनाना, paths check करना, file या folder rename करना, empty folder और file delete करना तथा os.path.join() से paths बनाना सीखा।
अब Final Coding Challenge पूरा करें और हर operation को अपनी practice directory में run करके देखें। Delete और rename जैसे operations को किसी जरूरी folder पर test न करें।
Related Articles — Python Hindii
- Python File Handling in Hindi: Core Concepts & Workflow
- Python File Reading in Hindi
- Python File Writing in Hindi — Create Files & write()
- Python Exception Handling in Hindi
- Python Strings in Hindi
- Python Lists Complete Guide in Hindi
- Python For Loop in Hindi
- Python Tutorial in Hindi — Complete Roadmap
- Online Python Editor & Compiler in Hindi
Python code likho, run karo aur practice karo — directly browser mein.
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें