Seedha content par jao
🤖 Python Hindi Bot
Namaste! Main Python Hindi Bot hoon 🐍
Python ya AI se related kuch bhi type karke pucho, ya neeche se quick sawaal chuno.
WhatsApp

Stay Connected with PythonHindii

Join our WhatsApp Community to get the latest Python tutorials, coding tips, interview questions, project updates, and learning resources directly on your phone.

Join WhatsApp Community
⇧
Python Hindi Logo
Python in Hindi | Programming Education
🎓 100% Free · Hindi me · Beginner se Career tak

Python Tutorial in HindiiLearn Python Programming for Free

Python, AI, Data Analytics aur SQL ek hi jagah, simple Hindi me. Padho, browser me code chalao, projects banao aur career shuru karo.

main.py
# Python seekho, Hindi me naam = "Ravi" for i in range(3): print("Namaste", naam)
▸ python main.py
🐍 Python 🤖 AI & ML 📊 Data 🗄️ SQL

Chahe Student ho ya Professional, yahan aapke liye sab hai

Apna card chuno, seedha sahi jagah pahunch jaoge.

🎓

Student ho?

School ya college me ho? Python zero se seekho, notes aur cheat sheets ke saath.

  • Python basics se OOP tak
  • Notes, PDFs aur Cheat Sheets
  • Browser me practice, kuch install nahi
💼

Working Professional ho?

Kaam ke saath nayi skill jodo: data, SQL aur AI se apna kaam tez karo.

  • Pandas, NumPy, Excel, Power BI
  • SQL aur MySQL databases
  • Automation aur AI Tools
🚀

Job ya Career change?

Projects banao, interview ki taiyari karo aur sahi roadmap follow karo.

  • Real projects, portfolio ke liye
  • Python aur SQL interview questions
  • AI Engineer, Data Scientist roadmap

Python Hindii Kaise Kaam Karta Hai?

Ek hi jagah 4 layers: seekho, practice karo, skills banao aur career shuru karo. Har layer ek step hai, us par click karke seedha wahan pahuncho. Computer par drag karke 3D me ghumao bhi.

Layer 1 · Seekho

Python Basics se Advanced tak

Variables, loops, functions se OOP tak, sab simple Hindi me. Yahin se shuru karo.

Naye ho? 3 step me shuru karo

💻 Online Code Editor

Browser me hi code likho aur turant chalao

Koi software install nahi. Code likho, Run dabao aur result usi waqt dekho.

  • Python, Java, C, C++, JavaScript, HTML aur aur bhi languages
  • input(), graphs aur multi-file HTML project bhi chalte hain
  • Mobile aur computer, dono par chalta hai
main.py
naam = input("Naam batao: ") for i in range(3): print("Namaste", naam)
▸ python main.py
PythonJavaCC++JavaScriptHTML

Python se AI tak ka poora rasta

8 steps, ek ke baad ek. Aage-peeche ghumao aur apni stage se seedha shuru karo.

Step 1 🐍

Python Basics

IntroductionVariablesData TypesOperatorsInput & OutputConditionsLoops
Start Python →
Step 2 💻

Python Intermediate

ListsTuplesSetsDictionariesStringsModulesFile Handling
Continue Python →
Step 4 🗄️

SQL & Databases

SQL BasicsMySQLSELECTINSERTUPDATEDELETEJOIN
Learn SQL →
Step 5 📊

Data Analytics

NumPyPandasData CleaningData VisualizationMatplotlibExploratory Data Analysis
Learn Data Analytics →
Step 6 🤖

Machine Learning

ML BasicsSupervised & Unsupervised LearningRegressionClassificationModel EvaluationScikit-learn
Learn Machine Learning →
Step 7 🧠

Artificial Intelligence

AI FundamentalsGenerative AIAI ToolsPrompt EngineeringLLM ConceptsAI Automation
Explore AI →
Step 8 🚀

Projects & Career

Python ProjectsAI ProjectsData ProjectsPortfolioGitHubInterview PreparationCareer Roadmap
Build Your Career →
Step 1 / 8

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 जरूर देखें।

📌 इस Lesson का Scope: इस lesson में केवल Python pathlib module के Path class के selected path, file और folder operations पर focus है। shutil, glob patterns, PurePath, file permissions और symbolic links को detail में नहीं पढ़ाया गया है।
🎥 Video में Topic समझें

Python pathlib Module क्या है?

pathlib Python की standard library का module है, जो file system paths को objects की तरह handle करता है। यहाँ path सिर्फ text नहीं रहता; वह एक Path object बन जाता है, जिस पर आप सीधे methods call कर सकते हैं, जैसे exists(), mkdir() या read_text()।

Simple Example
from pathlib import Path print(Path.cwd())

यह 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 के साथ काम करना।
Safety Tip: 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 लिखने की जरूरत नहीं पड़ती।

Example
from pathlib import Path print("pathlib ready") print("Current folder:", Path.cwd())

Possible Output:

pathlib ready Current folder: C:\Users\YourName\Documents\PythonProject

यह केवल example output है। आपके system में path अलग होगा।

Path() से Path Object बनाना

Path("notes.txt") लिखने पर Python एक Path Object बनाता है। ध्यान दें, Path object बनाने का मतलब यह नहीं कि file या folder असल में बन गया। यह सिर्फ एक path को represent करता है, चाहे वह मौजूद हो या नहीं।

Example
from pathlib import Path file_path = Path("notes.txt") print(file_path) print(type(file_path))

Possible Output:

notes.txt <class 'pathlib.PosixPath'>

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 करते हैं।

Example
from pathlib import Path print("Current Folder:", Path.cwd()) print("Home Folder:", Path.home())

Possible Output:

Current Folder: C:\Users\YourName\Documents\PythonProject Home Folder: C:\Users\YourName

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।
Example
from pathlib import Path p = Path("Projects/Python/report.txt") print("Name:", p.name) print("Stem:", p.stem) print("Suffix:", p.suffix) print("Parent:", p.parent) print("Parent name:", p.parent.name)

Possible Output:

Name: report.txt Stem: report Suffix: .txt Parent: Projects/Python Parent name: Python

Windows पर Parent path में backslash (\) दिख सकता है।

Tip: अगर file का नाम archive.tar.gz जैसा हो, तो suffix सिर्फ आखिरी extension (.gz) देता है। सभी extensions की list के लिए suffixes attribute इस्तेमाल करें।

/ Operator से Paths Combine करना

Path objects को जोड़ने के लिए pathlib में / operator इस्तेमाल होता है। यह os.path.join() के काम को ज्यादा readable तरीके से करता है, और separator operating system के अनुसार अपने-आप सही बनता है।

Example
from pathlib import Path folder = Path("Projects") file_path = folder / "Python" / "report.txt" print(file_path)

Possible Output on Linux/macOS:

Projects/Python/report.txt

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 है या नहीं।
Example
from pathlib import Path path = Path("notes.txt") if path.is_file(): print("This is a file") elif path.is_dir(): print("This is a folder") elif path.exists(): print("Path exists, but it is not a regular file or folder") else: print("Path not found")

ये 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 का इस्तेमाल किया जाता है।

Example — Single Folder
from pathlib import Path folder = Path("PythonPractice") folder.mkdir(exist_ok=True) print("Folder is ready!")
Example — Nested Folders
from pathlib import Path folder = Path("Projects/Python/Files") folder.mkdir(parents=True, exist_ok=True) print("Nested folders are ready!")

यह 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 इस्तेमाल कर सकते हैं।

Example
from pathlib import Path folder = Path(".") for item in sorted(folder.iterdir()): if item.is_dir(): print("Folder:", item.name) else: print("File:", item.name)

Possible Output:

File: main.py File: notes.txt Folder: practice

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" देना अच्छा तरीका है।

Example
from pathlib import Path file_path = Path("hello.txt") file_path.write_text("नमस्ते Python!\nHello pathlib!\n", encoding="utf-8") print("File written!")

Program की working directory में hello.txt बनेगी। यही code दोबारा चलाने पर file की पुरानी content बदल जाएगी।

read_text() — File की Text पढ़ना

read_text() पूरी text file को एक string के रूप में return करता है। यह छोटी text files के लिए सुविधाजनक है। File मौजूद न हो तो FileNotFoundError आ सकती है।

Example
from pathlib import Path file_path = Path("hello.txt") if file_path.is_file(): content = file_path.read_text(encoding="utf-8") print(content) else: print("hello.txt not found")

Possible Output:

नमस्ते Python! Hello pathlib!

बहुत बड़ी 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 — File Rename करना
from pathlib import Path old_path = Path("old_notes.txt") new_path = Path("new_notes.txt") if old_path.is_file(): result = old_path.rename(new_path) print("File renamed to:", result) else: print("old_notes.txt not found")

यह example तभी rename करेगा जब old_notes.txt current working directory में मौजूद हो।

ध्यान दें: अगर destination path पहले से मौजूद हो, तो behavior operating system के अनुसार अलग हो सकता है। Windows पर error आ सकती है, जबकि कुछ दूसरे systems पर existing file replace हो सकती है। जरूरी file खोने से बचने के लिए rename से पहले new_path.exists() check करें।

unlink() — File Delete करना

unlink() किसी file को delete करता है। यह os.remove() जैसा काम करता है। यह folder हटाने के लिए नहीं है; खाली folder हटाने के लिए rmdir() इस्तेमाल होता है।

Example — सावधानी के साथ File Delete करना
from pathlib import Path file_path = Path("temporary.txt") if file_path.is_file(): file_path.unlink() print("File deleted") else: print("File not found")
⚠️ Delete करने से पहले जाँचें

ऊपर का code वास्तविक file delete कर सकता है। इसे केवल अपनी test file और practice folder में चलाएँ। किसी महत्वपूर्ण file पर unlink() करने से पहले backup रखें।

Python 3.8 और उसके बाद unlink(missing_ok=True) देने पर file न होने पर FileNotFoundError नहीं आती। खाली folder delete करने के लिए इस तरह लिख सकते हैं:

from pathlib import Path folder = Path("EmptyFolder") if folder.is_dir() and not any(folder.iterdir()): folder.rmdir() print("Empty folder deleted") else: print("Folder missing or not empty")

os module और pathlib की तुलना

अगर आप os module पहले सीख चुके हैं, तो नीचे की table से आप दोनों के बीच का रिश्ता आसानी से समझ सकते हैं।

कामos modulepathlib
Current directoryos.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 की listos.listdir()p.iterdir()
Renameos.rename()p.rename()
File deleteos.remove()p.unlink()
खाली folder deleteos.rmdir()p.rmdir()
क्या os module अब जरूरी नहीं? Path और file/folder operations के लिए 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 में चलाएँ।

Example — Project Folder Setup with pathlib
from pathlib import Path # 1. Project folder और उसके अंदर notes folder बनाना base_folder = Path("PythonProject") notes_folder = base_folder / "notes" notes_folder.mkdir(parents=True, exist_ok=True) # 2. File का पूरा path बनाना file_path = notes_folder / "welcome.txt" # 3. File में text लिखना file_path.write_text( "Welcome to Python pathlib!\nThis file was created using Path.write_text().\n", encoding="utf-8" ) # 4. Path check करना if file_path.is_file(): print("File created:", file_path) print("File name:", file_path.name) print("Extension:", file_path.suffix) else: print("File was not created") # 5. File की content पढ़ना print(file_path.read_text(encoding="utf-8")) # 6. Project folder की entries देखना for item in sorted(base_folder.iterdir()): print("Project item:", item.name)

Possible Output:

File created: PythonProject/notes/welcome.txt File name: welcome.txt Extension: .txt Welcome to Python pathlib! This file was created using Path.write_text(). Project item: notes

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

❌ 1. Path object में + से string जोड़ना
from pathlib import Path p = Path("Projects") + "notes.txt"

इससे TypeError आती है, क्योंकि Path object के साथ + से string नहीं जुड़ती।

✅ सही तरीका
p = Path("Projects") / "notes.txt"

Paths जोड़ने के लिए / operator इस्तेमाल करें।

❌ 2. Path object बनाने को file बनना समझ लेना
file_path = Path("data.txt") print(file_path.read_text())

Path("data.txt") सिर्फ path object है। अगर file मौजूद नहीं है, तो read_text() पर FileNotFoundError आएगी।

✅ सही तरीका
file_path = Path("data.txt") if file_path.is_file(): print(file_path.read_text(encoding="utf-8")) else: print("data.txt not found")
❌ 3. Nested folder बिना parents=True के बनाना
Path("Projects/Python/Files").mkdir()

अगर Projects या Projects/Python मौजूद नहीं है, तो FileNotFoundError आ सकती है। और अगर पूरा folder पहले से मौजूद है, तो FileExistsError आ सकती है।

✅ सही तरीका
Path("Projects/Python/Files").mkdir(parents=True, exist_ok=True)
❌ 4. write_text() से पुरानी content खो देना
Path("notes.txt").write_text("New content")

अगर notes.txt पहले से मौजूद है, तो उसकी पुरानी content replace हो जाती है।

✅ सही तरीका
file_path = Path("notes.txt") if file_path.exists(): print("File already exists. Overwrite करने से पहले check करें।") else: file_path.write_text("New content", encoding="utf-8")

पुरानी content के आगे text जोड़ना हो, तो open(file_path, "a", encoding="utf-8") इस्तेमाल करें।

❌ 5. unlink() से folder delete करने की कोशिश करना

unlink() files के लिए है, folders के लिए नहीं। Folder पर इसे चलाने से error आ सकती है।

✅ सही तरीका

File के लिए unlink() और खाली folder के लिए rmdir() इस्तेमाल करें। Delete करने से पहले target path को verify करें।

❌ 6. iterdir() के क्रम पर निर्भर रहना
for item in folder.iterdir(): print(item.name)

Entries का क्रम guaranteed नहीं होता। अलग computer पर output का क्रम अलग हो सकता है।

✅ सही तरीका
for item in sorted(folder.iterdir()): print(item.name)

Quick Exercise 🧠

Path objects को जोड़ने के लिए pathlib में कौन सा operator इस्तेमाल होता है?

A) +
B) *
C) /

सही उत्तर: C — / operator

Final Coding Challenge

अब VS Code में pathlib_practice.py नाम की file बनाएँ और नीचे दिए गए tasks पूरा करें। Rename और delete वाले tasks के लिए केवल अपनी बनाई हुई practice files का इस्तेमाल करें।

  1. from pathlib import Path करके Path.cwd() और Path.home() print करें।
  2. Path("MyPathPractice/Files/Notes/todo.txt") का name, stem, suffix और parent print करें।
  3. / operator से MyPathPractice के अंदर Files और Notes का path बनाएँ।
  4. mkdir(parents=True, exist_ok=True) से nested folders बनाएँ।
  5. write_text() से todo.txt में 3 lines लिखें (कम से कम एक Hindi line के साथ)।
  6. read_text() से file की content पढ़कर print करें।
  7. exists(), is_file() और is_dir() से file और folder दोनों check करें।
  8. iterdir() और sorted() से Notes folder की entries print करें।
  9. todo.txt को rename() से tasks.txt बनाएँ, फिर unlink() से हटाएँ।
  10. Bonus: हर operation के बाद status message print करें और गलत path पर आने वाली error को समझें।

Quick Revision

Syntaxकाम
from pathlib import PathPath class import करता है।
Path("file.txt")Path object बनाता है (file नहीं बनाता)।
Path.cwd()Current working directory देता है।
Path.home()User की home directory देता है।
p.nameFile या folder का नाम देता है।
p.stemExtension के बिना नाम देता है।
p.suffixFile extension देता है।
p.parentParent 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 Coding Lab

Python code likho, run karo aur practice karo — directly browser mein.