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 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 भी पढ़ें।

📌 इस Lesson का Scope: इस lesson में केवल Python os module के selected file, folder और directory operations पर focus है। shutil, pathlib, sys, processes और environment variables को detail में नहीं पढ़ाया गया है।
🎥 Video में Topic समझें

Python os Module क्या है?

os Python का built-in module है, जो operating system से संबंधित कई कामों के लिए functions देता है। इससे आप directories के साथ काम कर सकते हैं, files और folders की जानकारी ले सकते हैं, directory बना सकते हैं और file या folder का नाम बदल सकते हैं।

Simple Example
import os print(os.getcwd())

यह 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 बनाना।
Safety Tip: File rename और delete करने वाले examples पहले practice folder में चलाएँ। खासकर os.remove() और os.rmdir() इस्तेमाल करने से पहले path को ध्यान से जाँचें।

import os क्या करता है?

Python में module के functions इस्तेमाल करने से पहले उसे import करना पड़ता है। import os लिखने के बाद आप os.getcwd() जैसे functions call कर सकते हैं।

Example
import os print("OS module ready") print("Current folder:", os.getcwd())

Possible Output:

OS module ready Current folder: C:\Users\YourName\Documents\PythonProject

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

os.getcwd() — Current Working Directory

os.getcwd() का पूरा नाम current working directory है। यह बताता है कि Python program इस समय किस directory को अपनी working location मानकर चल रहा है। Relative file paths अक्सर इसी directory के आधार पर resolve होते हैं।

Example
import os current_folder = os.getcwd() print("Current Working Directory:", current_folder)
यदि कोई file आपकी Python script के साथ वाले folder में दिखाई नहीं दे रही, तो जरूरी नहीं कि वह बनी ही न हो। संभव है program किसी दूसरी working directory से run हुआ हो। os.getcwd() से इसे check करें।

os.chdir() — Directory बदलना

os.chdir() current working directory बदलता है। इसे उस directory के path के साथ call किया जाता है जहाँ आप आगे काम करना चाहते हैं। Directory मौजूद न हो या access न मिले, तो error आ सकती है।

Example
import os print("Before:", os.getcwd()) # अपने computer पर मौजूद valid path दें # os.chdir(r"C:\Users\YourName\Documents") print("After:", os.getcwd())

ऊपर os.chdir() की line comment है, ताकि किसी गलत या non-existent path पर अनजाने में error न आए। अपने computer पर सही path देकर # हटाएँ।

Windows Tip: Windows path लिखते समय raw string जैसे 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 दोनों के नाम आ सकते हैं।

Example
import os items = os.listdir() print(items)

Possible Output:

['main.py', 'notes.txt', 'images', 'practice']

Output आपके folder की वास्तविक files और folders पर निर्भर करेगा।

किसी खास directory की list देखनी हो, तो उसका path दें:

import os items = os.listdir("practice") for item in items: print(item)

os.mkdir() — नया Folder बनाना

os.mkdir() एक नया directory/folder बनाता है। जिस parent directory में folder बनाना है, वह पहले से मौजूद होनी चाहिए। अगर उसी नाम का folder पहले से मौजूद है, तो सामान्यतः FileExistsError आ सकती है।

Example
import os os.mkdir("PythonPractice") print("Folder created!")

Program की working directory में PythonPractice नाम का folder बनेगा। इस code को दोबारा चलाने से पहले देखें कि folder पहले से मौजूद तो नहीं है।

os.makedirs() — Nested Folders बनाना

जब एक साथ कई levels के folders बनाने हों, तो os.makedirs() इस्तेमाल कर सकते हैं। यह जरूरत पड़ने पर बीच के parent folders भी बना सकता है।

Example
import os os.makedirs("Projects/Python/Files", exist_ok=True) print("Nested folders are ready!")

यह 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 मिलता है।

Example
import os path = "notes.txt" if os.path.exists(path): print("Path exists") else: print("Path not found")

इस 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 करता है या नहीं।
Example
import os path = "notes.txt" if os.path.isfile(path): print("This is a file") elif os.path.isdir(path): print("This is a folder") else: print("Path does not exist as a file or folder")

os.rename() — File या Folder Rename करना

os.rename(old, new) किसी file या directory का नाम बदलने के लिए इस्तेमाल होता है। पुराने path का मौजूद होना जरूरी है, और नए path की स्थिति तथा permissions के अनुसार operation सफल या असफल हो सकता है।

Example — File Rename करना
import os if os.path.isfile("old_notes.txt"): os.rename("old_notes.txt", "new_notes.txt") print("File renamed!") else: print("old_notes.txt not found")

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

ध्यान दें: Rename करते समय destination path पहले से मौजूद हो तो behavior operating system और परिस्थितियों के अनुसार अलग हो सकता है। जरूरी file को overwrite या move होने से बचाने के लिए destination पहले check करें।

os.rmdir() — Empty Folder Delete करना

os.rmdir() किसी खाली directory को हटाता है। यदि folder में files या subfolders मौजूद हैं, तो यह सामान्यतः OSError देगा। यह function folder के अंदर की सामग्री को recursively delete नहीं करता।

Example
import os folder = "EmptyFolder" if os.path.isdir(folder) and not os.listdir(folder): os.rmdir(folder) print("Empty folder deleted") else: print("Folder missing or not empty")

os.remove() — File Delete करना

os.remove() किसी file को delete करता है। यह directory को delete करने के लिए नहीं है। Delete करने के बाद file वापस पाना आसान न भी हो, इसलिए पहले path और file name को ध्यान से check करें।

Example — सावधानी के साथ File Delete करना
import os file_name = "temporary.txt" if os.path.isfile(file_name): os.remove(file_name) print("File deleted") else: print("File not found")
⚠️ Delete करने से पहले जाँचें

ऊपर का 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 के अनुसार अलग हो सकता है।

Example
import os folder = "Projects" file_name = "report.txt" full_path = os.path.join(folder, file_name) print(full_path)

Possible Output on Windows:

Projects\report.txt

दूसरे operating systems पर separator अलग हो सकता है।

इसका उपयोग directory check, file open या file rename जैसे operations में भी कर सकते हैं:

import os folder = "Projects" file_path = os.path.join(folder, "report.txt") if os.path.exists(file_path): print("Report found") else: print("Report not found")

Real-world File और Folder Management Example

अब एक छोटा example बनाते हैं जो project folder तैयार करेगा, उसमें notes folder बनाएगा, text file create करेगा और फिर path check करेगा। यह example केवल अपनी practice directory में चलाएँ।

Example — Project Folder Setup
import os # 1. Project folder और उसके अंदर notes folder बनाना base_folder = "PythonProject" notes_folder = os.path.join(base_folder, "notes") os.makedirs(notes_folder, exist_ok=True) # 2. File का पूरा path बनाना file_path = os.path.join(notes_folder, "welcome.txt") # 3. File में text लिखना with open(file_path, "w", encoding="utf-8") as file: file.write("Welcome to Python file management!\n") file.write("This file was created using os and open().\n") # 4. Path check करना if os.path.isfile(file_path): print("File created:", file_path) else: print("File was not created") # 5. Project folder की entries देखना print("Project contents:", os.listdir(base_folder))

Possible Output:

File created: PythonProject/notes/welcome.txt Project contents: ['notes']

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

❌ 1. गलत directory path देना
os.chdir("WrongFolder")

यदि folder मौजूद नहीं है या path गलत है, तो FileNotFoundError आ सकती है।

✅ सही तरीका

os.getcwd() से current directory check करें और os.chdir() को सही, मौजूद path दें।

❌ 2. मौजूद folder को फिर से os.mkdir() से बनाना
os.mkdir("PythonPractice")

अगर folder पहले से मौजूद है, तो FileExistsError आ सकती है।

✅ सही तरीका
import os if not os.path.exists("PythonPractice"): os.mkdir("PythonPractice")

Nested directories के लिए os.makedirs("Projects/Python", exist_ok=True) उपयोगी है।

❌ 3. Non-empty folder को os.rmdir() से हटाना

os.rmdir() केवल खाली directory हटाता है। Folder के अंदर files हों, तो operation fail हो सकता है।

✅ सही तरीका
if os.path.isdir("EmptyFolder") and not os.listdir("EmptyFolder"): os.rmdir("EmptyFolder")

इस lesson में केवल empty folder deletion शामिल है। Recursive folder deletion के लिए दूसरे tools और अतिरिक्त सावधानी की जरूरत होती है।

❌ 4. os.remove() से folder delete करने की कोशिश करना

os.remove() files के लिए है, directories के लिए नहीं।

✅ सही तरीका

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

❌ 5. हर OS पर एक ही slash वाला path मान लेना

Windows और Unix-like operating systems में path separators अलग हो सकते हैं।

✅ सही तरीका
path = os.path.join("Projects", "Python", "notes.txt")

Quick Exercise 🧠

किस function से current working directory का path मिलता है?

A) os.listdir()
B) os.getcwd()
C) os.mkdir()

सही उत्तर: B — os.getcwd()

Final Coding Challenge

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

  1. import os करके current working directory print करें।
  2. os.listdir() से current directory की entries print करें।
  3. os.mkdir() से MyOSPractice नाम का folder बनाएँ; दोबारा run करने से पहले existence check करें।
  4. os.makedirs() से MyOSPractice/Files/Notes nested structure बनाएँ और exist_ok=True आजमाएँ।
  5. os.path.join() से notes.txt का path बनाएँ।
  6. os.path.exists(), os.path.isfile() और os.path.isdir() का इस्तेमाल करके paths check करें।
  7. Practice folder के अंदर एक text file बनाएँ और उसका नाम os.rename() से बदलें।
  8. एक खाली folder बनाएँ और os.rmdir() से उसे हटाएँ।
  9. एक temporary practice file बनाएँ, verify करें और फिर os.remove() से delete करें।
  10. 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 Coding Lab

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