Python pathlib Module in Hindi: Path, mkdir, read_text सीखें
Python pathlib Module क्या है? Path Object से Files और Folders Manage करना
Python में files और folders के साथ काम करते समय सबसे पहले जरूरत पड़ती है path की। अगर path को सिर्फ string की तरह संभालें, तो slash जोड़ना, file का नाम निकालना, extension पता करना और OS के अनुसार separator सही रखना जल्दी उलझन भरा हो जाता है। इसी समस्या को आसान बनाने के लिए Python में built-in pathlib module मौजूद है।
इस tutorial में आप सीखेंगे कि from pathlib import Path कैसे काम करता है, Path() से Path Object कैसे बनता है, Path.cwd() और Path.home() क्या देते हैं, name, stem, suffix और parent से path की जानकारी कैसे निकलती है, / operator से paths कैसे जुड़ते हैं, और exists(), mkdir(), iterdir(), read_text(), write_text(), rename() तथा unlink() का इस्तेमाल कैसे होता है। सभी examples beginner-friendly हैं।
यह lesson पिछले Python os Module in Hindi lesson का modern follow-up है। अगर आपने file handling की basics अभी नहीं पढ़ीं, तो पहले Python File Handling in Hindi: Core Concepts & Workflow, Python File Reading in Hindi और Python File Writing in Hindi जरूर देखें।
pathlib module के Path class के selected path, file और folder operations पर focus है। shutil, glob patterns, PurePath, file permissions और symbolic links को detail में नहीं पढ़ाया गया है।
Python pathlib Module क्या है?
pathlib Python की standard library का module है, जो file system paths को objects की तरह handle करता है। यहाँ path सिर्फ text नहीं रहता; वह एक Path object बन जाता है, जिस पर आप सीधे methods call कर सकते हैं, जैसे exists(), mkdir() या read_text()।
यह code current working directory का path print करता है। आपके computer पर output अलग हो सकता है।
pathlib की जरूरत क्यों पड़ती है?
मान लीजिए आपको Projects/Python/report.txt जैसी file का नाम, extension और parent folder निकालना है। String methods से यह काम संभव है, लेकिन code लंबा और गलती-प्रवण हो सकता है। pathlib में यही जानकारी सीधे attributes से मिल जाती है।
- Path को string की जगह object की तरह इस्तेमाल करना।
/operator से paths को साफ तरीके से जोड़ना।- File name, stem, extension और parent folder निकालना।
- File या folder मौजूद है या नहीं, check करना।
- Folder और nested folders बनाना।
- Folder की entries पर loop चलाना।
- Text file को सीधे पढ़ना और लिखना।
- File rename और delete करना।
- Windows, Linux और macOS पर सही path separator के साथ काम करना।
write_text(), rename() और unlink() वाले examples पहले practice folder में चलाएँ। ये operations existing files को बदल या हटा सकते हैं।from pathlib import Path क्या करता है?
pathlib module में कई classes हैं, लेकिन beginners के लिए सबसे जरूरी class Path है। from pathlib import Path लिखने से आप सीधे Path नाम इस्तेमाल कर सकते हैं, बार-बार pathlib.Path लिखने की जरूरत नहीं पड़ती।
Possible Output:
यह केवल example output है। आपके system में path अलग होगा।
Path() से Path Object बनाना
Path("notes.txt") लिखने पर Python एक Path Object बनाता है। ध्यान दें, Path object बनाने का मतलब यह नहीं कि file या folder असल में बन गया। यह सिर्फ एक path को represent करता है, चाहे वह मौजूद हो या नहीं।
Possible Output:
Windows पर class का नाम pathlib.WindowsPath दिख सकता है। दोनों में आपके लिए code लिखने का तरीका समान रहता है।
Path("notes.txt") सिर्फ path object बनाता है। File बनाने के लिए write_text() या touch() जैसे methods का इस्तेमाल करना पड़ता है।Path.cwd() और Path.home()
Path.cwd() current working directory देता है, यानी वह folder जहाँ से Python program चल रहा है। Path.home() current user की home directory देता है। दोनों Path object return करते हैं।
Possible Output:
Output आपके computer के user और folder structure पर निर्भर करेगा।
Path.cwd() का काम os.getcwd() जैसा है, लेकिन return value string की जगह Path object होती है।
name, stem, suffix और parent
Path object से file या folder की जानकारी निकालने के लिए चार attributes बहुत काम आते हैं:
name— path का आखिरी हिस्सा, यानी file या folder का नाम (extension सहित)।stem— extension के बिना file name।suffix— file extension, जैसे.txt।parent— उस path का parent folder।
Possible Output:
Windows पर Parent path में backslash (\) दिख सकता है।
archive.tar.gz जैसा हो, तो suffix सिर्फ आखिरी extension (.gz) देता है। सभी extensions की list के लिए suffixes attribute इस्तेमाल करें।/ Operator से Paths Combine करना
Path objects को जोड़ने के लिए pathlib में / operator इस्तेमाल होता है। यह os.path.join() के काम को ज्यादा readable तरीके से करता है, और separator operating system के अनुसार अपने-आप सही बनता है।
Possible Output on Linux/macOS:
Windows पर यही path Projects\Python\report.txt की तरह दिखेगा।
यहाँ / division नहीं कर रहा; Path class ने इस operator को path जोड़ने के लिए define किया है। Left side में Path object होना चाहिए और right side में string या दूसरा Path object चल सकता है।
exists(), is_file() और is_dir()
तीनों methods path की स्थिति check करने में मदद करते हैं:
exists()— path पर कोई file या folder मौजूद है या नहीं।is_file()— path एक मौजूद regular file है या नहीं।is_dir()— path एक मौजूद directory/folder है या नहीं।
ये methods os.path.exists(), os.path.isfile() और os.path.isdir() की तरह ही True या False देते हैं। फर्क बस इतना है कि यहाँ function में path pass करने की जगह path object पर method call होता है। ध्यान रखें कि check के बाद भी file system बदल सकता है, इसलिए महत्वपूर्ण operations में errors handle करना अच्छा रहता है।
mkdir() — Folder बनाना
mkdir() एक नया folder बनाता है। Nested folders बनाने के लिए parents=True और पहले से मौजूद folder पर error से बचने के लिए exist_ok=True का इस्तेमाल किया जाता है।
यह Projects, उसके अंदर Python और उसके अंदर Files folder बनाता है। parents=True बीच के folders बनाता है और exist_ok=True होने पर target folder पहले से मौजूद होने पर error नहीं आती।
इस तरह Path.mkdir() एक ही method में os.mkdir() और os.makedirs() दोनों की जरूरत पूरी कर देता है।
iterdir() — Folder की Entries देखना
iterdir() किसी directory के अंदर मौजूद files और folders को एक-एक करके देता है। हर entry एक Path object होती है, इसलिए आप उसी पर name, is_file() या is_dir() जैसे attributes और methods इस्तेमाल कर सकते हैं।
Possible Output:
Output आपके folder की वास्तविक files और folders पर निर्भर करेगा।
iterdir() जिस क्रम में entries देता है, वह guaranteed नहीं होता। क्रम चाहिए तो ऊपर की तरह sorted() इस्तेमाल करें। Folder मौजूद न हो तो iterdir() से error आ सकती है।write_text() — File में Text लिखना
write_text() Path object वाली file में text लिखता है। File न हो तो नई file बन जाती है, और file पहले से मौजूद हो तो उसकी पुरानी content replace हो जाती है। Hindi text के लिए encoding="utf-8" देना अच्छा तरीका है।
Program की working directory में hello.txt बनेगी। यही code दोबारा चलाने पर file की पुरानी content बदल जाएगी।
read_text() — File की Text पढ़ना
read_text() पूरी text file को एक string के रूप में return करता है। यह छोटी text files के लिए सुविधाजनक है। File मौजूद न हो तो FileNotFoundError आ सकती है।
Possible Output:
बहुत बड़ी files को line-by-line पढ़ना हो, तो open() के साथ loop इस्तेमाल करना बेहतर रहता है। Path object को open() में भी दिया जा सकता है। इस बारे में विस्तार से Python File Reading in Hindi और Python File Writing in Hindi में पढ़ें।
rename() — File या Folder Rename करना
rename() path का नाम बदलता है और नए path का Path object return करता है। पुराना path मौजूद होना चाहिए।
यह example तभी rename करेगा जब old_notes.txt current working directory में मौजूद हो।
new_path.exists() check करें।unlink() — File Delete करना
unlink() किसी file को delete करता है। यह os.remove() जैसा काम करता है। यह folder हटाने के लिए नहीं है; खाली folder हटाने के लिए rmdir() इस्तेमाल होता है।
ऊपर का code वास्तविक file delete कर सकता है। इसे केवल अपनी test file और practice folder में चलाएँ। किसी महत्वपूर्ण file पर unlink() करने से पहले backup रखें।
Python 3.8 और उसके बाद unlink(missing_ok=True) देने पर file न होने पर FileNotFoundError नहीं आती। खाली folder delete करने के लिए इस तरह लिख सकते हैं:
os module और pathlib की तुलना
अगर आप os module पहले सीख चुके हैं, तो नीचे की table से आप दोनों के बीच का रिश्ता आसानी से समझ सकते हैं।
| काम | os module | pathlib |
|---|---|---|
| Current directory | os.getcwd() | Path.cwd() |
| Path जोड़ना | os.path.join(a, b) | Path(a) / b |
| Path मौजूद है? | os.path.exists(p) | p.exists() |
| File है? | os.path.isfile(p) | p.is_file() |
| Folder है? | os.path.isdir(p) | p.is_dir() |
| Folder बनाना | os.mkdir() / os.makedirs() | p.mkdir(parents=True, exist_ok=True) |
| Entries की list | os.listdir() | p.iterdir() |
| Rename | os.rename() | p.rename() |
| File delete | os.remove() | p.unlink() |
| खाली folder delete | os.rmdir() | p.rmdir() |
pathlib अक्सर ज्यादा साफ और modern तरीका है, लेकिन os module का काम सिर्फ paths तक सीमित नहीं है। Environment variables, processes और दूसरे system-level काम के लिए आपको os की जरूरत पड़ सकती है। आधिकारिक जानकारी के लिए Python pathlib documentation देखें।Real-world File और Folder Management Example
अब एक छोटा example बनाते हैं जो project folder तैयार करेगा, उसमें notes folder बनाएगा, text file लिखेगा, उसे पढ़ेगा और folder की entries दिखाएगा। यह example केवल अपनी practice directory में चलाएँ।
Possible Output:
Windows पर path में backslash दिख सकता है, और output आपके system के अनुसार थोड़ा अलग हो सकता है।
इस example में mkdir() folder structure बनाता है, / operator paths जोड़ता है, write_text() और read_text() file पर काम करते हैं, is_file() existence check करता है और iterdir() folder की entries देता है।
Common Mistakes
इससे TypeError आती है, क्योंकि Path object के साथ + से string नहीं जुड़ती।
Paths जोड़ने के लिए / operator इस्तेमाल करें।
Path("data.txt") सिर्फ path object है। अगर file मौजूद नहीं है, तो read_text() पर FileNotFoundError आएगी।
अगर Projects या Projects/Python मौजूद नहीं है, तो FileNotFoundError आ सकती है। और अगर पूरा folder पहले से मौजूद है, तो FileExistsError आ सकती है।
अगर notes.txt पहले से मौजूद है, तो उसकी पुरानी content replace हो जाती है।
पुरानी content के आगे text जोड़ना हो, तो open(file_path, "a", encoding="utf-8") इस्तेमाल करें।
unlink() files के लिए है, folders के लिए नहीं। Folder पर इसे चलाने से error आ सकती है।
File के लिए unlink() और खाली folder के लिए rmdir() इस्तेमाल करें। Delete करने से पहले target path को verify करें।
Entries का क्रम guaranteed नहीं होता। अलग computer पर output का क्रम अलग हो सकता है।
Quick Exercise 🧠
Path objects को जोड़ने के लिए pathlib में कौन सा operator इस्तेमाल होता है?
+*/सही उत्तर: C — / operator
Final Coding Challenge
अब VS Code में pathlib_practice.py नाम की file बनाएँ और नीचे दिए गए tasks पूरा करें। Rename और delete वाले tasks के लिए केवल अपनी बनाई हुई practice files का इस्तेमाल करें।
from pathlib import PathकरकेPath.cwd()औरPath.home()print करें।Path("MyPathPractice/Files/Notes/todo.txt")काname,stem,suffixऔरparentprint करें।/operator सेMyPathPracticeके अंदरFilesऔरNotesका path बनाएँ।mkdir(parents=True, exist_ok=True)से nested folders बनाएँ।write_text()सेtodo.txtमें 3 lines लिखें (कम से कम एक Hindi line के साथ)।read_text()से file की content पढ़कर print करें।exists(),is_file()औरis_dir()से file और folder दोनों check करें।iterdir()औरsorted()से Notes folder की entries print करें।todo.txtकोrename()सेtasks.txtबनाएँ, फिरunlink()से हटाएँ।- Bonus: हर operation के बाद status message print करें और गलत path पर आने वाली error को समझें।
Quick Revision
| Syntax | काम |
|---|---|
from pathlib import Path | Path class import करता है। |
Path("file.txt") | Path object बनाता है (file नहीं बनाता)। |
Path.cwd() | Current working directory देता है। |
Path.home() | User की home directory देता है। |
p.name | File या folder का नाम देता है। |
p.stem | Extension के बिना नाम देता है। |
p.suffix | File extension देता है। |
p.parent | Parent folder का path देता है। |
Path("a") / "b" | Paths को जोड़ता है। |
p.exists() | Path मौजूद है या नहीं check करता है। |
p.is_file() | Path regular file है या नहीं check करता है। |
p.is_dir() | Path folder है या नहीं check करता है। |
p.mkdir(parents=True, exist_ok=True) | Nested folders बनाता है। |
p.iterdir() | Folder की entries देता है। |
p.write_text(text) | File में text लिखता है (पुरानी content replace)। |
p.read_text() | File की पूरी text पढ़ता है। |
p.rename(new) | File या folder rename करता है। |
p.unlink() | File delete करता है। |
p.rmdir() | खाली folder delete करता है। |
FAQ — Python pathlib Module
1. Python में pathlib module क्या है?
pathlib Python की standard library का module है, जो file और folder paths को string की जगह object-oriented तरीके से manage करने देता है। Path object के methods से आप path check, folder create, file read/write, rename और delete जैसे काम कर सकते हैं।
2. क्या pathlib module install करना पड़ता है?
नहीं। pathlib Python 3.4 से standard library में शामिल है। सामान्यतः केवल from pathlib import Path लिखना होता है।
3. pathlib और os module में क्या फर्क है?
os और os.path में ज्यादातर functions string paths के साथ काम करते हैं, जबकि pathlib में path एक Path object होता है और उसी object पर methods call किए जाते हैं। Modern Python code में pathlib अक्सर ज्यादा साफ और पढ़ने में आसान माना जाता है।
4. क्या pathlib आने के बाद os module बेकार हो गया?
नहीं। os module में environment variables, processes और कई दूसरे system-level functions भी हैं, जो pathlib का काम नहीं हैं। लेकिन file paths और file/folder operations के लिए pathlib एक आधुनिक और सुविधाजनक विकल्प है।
5. Path.cwd() और Path.home() क्या करते हैं?
Path.cwd() current working directory का path देता है और Path.home() current user की home directory का path देता है। दोनों Path object return करते हैं।
6. Path में / operator क्या करता है?
/ operator path parts को जोड़ता है। जैसे Path("Projects") / "notes.txt" एक नया Path object बनाता है, जिसमें separator operating system के अनुसार सही रहता है।
7. pathlib से file exists है या नहीं कैसे check करें?
path.exists() से check करें कि path मौजूद है या नहीं। विशेष रूप से file के लिए path.is_file() और folder के लिए path.is_dir() इस्तेमाल करें।
8. pathlib में folder कैसे बनाते हैं?
path.mkdir() से folder बनता है। Nested folders के लिए path.mkdir(parents=True, exist_ok=True) इस्तेमाल करें।
9. read_text() और write_text() कब इस्तेमाल करें?
छोटी text files को एक साथ पढ़ने या लिखने के लिए read_text() और write_text() सुविधाजनक हैं। ध्यान रखें कि write_text() file की पुरानी content को replace कर देता है।
10. pathlib में file delete कैसे करें?
File delete करने के लिए path.unlink() और खाली folder delete करने के लिए path.rmdir() इस्तेमाल होता है। Delete से पहले path verify करें और जरूरी files का backup रखें।
Summary
Python pathlib Module = Path को object की तरह इस्तेमाल करके files और folders को साफ और modern तरीके से manage करना।
इस lesson में आपने from pathlib import Path, Path object बनाना, Path.cwd() और Path.home(), name, stem, suffix और parent, / operator से paths जोड़ना, exists(), is_file() और is_dir(), mkdir() और iterdir(), read_text() और write_text() तथा rename() और unlink() सीखे।
अब Final Coding Challenge पूरा करें और हर operation को अपनी practice directory में run करके देखें। अगर आपको os module और pathlib की तुलना दोबारा देखनी हो, तो Python os Module in Hindi lesson फिर से पढ़ें।
Related Articles — Python Hindii
- Python os Module in Hindi — Files, Folders और Directories Manage करें
- 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.
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें