|
प्रश्न 1. Python में Folder बनाने के लिए कौन-सा Function है? |
Python में कई बार हमें Operating System से जुड़े कार्य Program के माध्यम से करने पड़ते हैं। जैसे किसी Folder की Location पता करना, Files और Folders की List देखना, नया Folder बनाना, Folder बदलना, File Rename करना या File Delete करना।
इन कार्यों के लिए Python का os Module बहुत उपयोगी है।
os Python की Standard Library का हिस्सा है।
import os
os का अर्थ Operating System है।
Python का os Module Operating System के साथ Interaction करने के लिए विभिन्न Functions और सुविधाएँ प्रदान करता है।
इसके माध्यम से हम—
- Current Working Directory देख सकते हैं।
- Files और Folders की List प्राप्त कर सकते हैं।
- Directory बना सकते हैं।
- Directory बदल सकते हैं।
- File/Folder Rename कर सकते हैं।
- File Delete कर सकते हैं।
- Folder Delete कर सकते हैं।
- Path की जानकारी प्राप्त कर सकते हैं।
getcwd() का अर्थ है Get Current Working Directory।
यह उस Folder का पूरा Path बताता है जहाँ से Python का Current Process काम कर रहा है।
import os
print(os.getcwd())
संभावित Output:
C:\Users\Rahul\Python
listdir() किसी Directory के अंदर मौजूद Files और Folders की List देता है।
import os
print(os.listdir())
यह Current Working Directory की Contents दिखाएगा।
import os
print(os.listdir("Documents"))
यदि Folder मौजूद है, तो उसके अंदर की Files और Directories की List मिल जाएगी।
mkdir() का उपयोग एक नया Directory/Folder बनाने के लिए किया जाता है।
import os
os.mkdir("MyFolder")
इससे Current Directory के अंदर MyFolder नाम का नया Folder बनेगा।
यदि उसी नाम का Folder पहले से मौजूद है, तो Error आ सकता है।
makedirs() की सहायता से एक साथ कई स्तरों वाली Directories बनाई जा सकती हैं।
import os
os.makedirs("College/Students/2026")
इससे आवश्यक Parent Folders भी बनाए जा सकते हैं।
mkdir() और makedirs() में अंतर
|
mkdir() |
makedirs() |
|
एक Directory बनाने के लिए |
Nested Directories बनाने के लिए |
|
Parent Directory मौजूद होना चाहिए |
आवश्यक Parent Directories भी बना सकता है |
|
mkdir("Test") |
makedirs("A/B/C") |
chdir() का अर्थ Change Directory है।
इसका उपयोग Current Working Directory बदलने के लिए किया जाता है।
import os
os.chdir("C:\\Users\\Rahul\\Documents")
print(os.getcwd())
Windows Path लिखते समय Raw String जैसे r"C:\Users\Rahul\Documents" का उपयोग करना सुविधाजनक होता है।
rename() का उपयोग File या Directory का नाम बदलने के लिए किया जाता है।
import os
os.rename("old.txt", "new.txt")
os.rename("OldFolder", "NewFolder")
remove() किसी File को Delete करने के लिए उपयोग किया जाता है।
import os
os.remove("test.txt")
os.remove() सामान्यतः File हटाने के लिए है, Directory हटाने के लिए नहीं।
rmdir() का उपयोग Empty Directory को Delete करने के लिए किया जाता है।
import os
os.rmdir("MyFolder")
यदि Folder के अंदर Files या अन्य Folders मौजूद हैं, तो rmdir() काम नहीं करेगा।
|
remove() |
rmdir() |
|
File Delete करता है |
Empty Directory Delete करता है |
|
os.remove() |
os.rmdir() |
|
File के लिए |
Folder के लिए |
यह Check करता है कि दिया गया File या Path मौजूद है या नहीं।
import os
if os.path.exists("test.txt"):
print("File exists")
else:
print("File does not exist")
यह Check करता है कि दिया गया Path एक File है या नहीं।
import os
if os.path.isfile("test.txt"):
print("It is a file")
यह Check करता है कि दिया गया Path एक Directory है या नहीं।
import os
if os.path.isdir("Documents"):
print("It is a directory")
किसी File का Size Bytes में प्राप्त करने के लिए—
import os
size = os.path.getsize("test.txt")
print("Size:", size, "bytes")
किसी Path से केवल अंतिम File या Directory का नाम प्राप्त करने के लिए।
import os
path = "C:\\Users\\Rahul\\Documents\\test.txt"
print(os.path.basename(path))
Output:
test.txt
किसी Path से Directory वाला भाग प्राप्त करने के लिए।
import os
path = "C:\\Users\\Rahul\\Documents\\test.txt"
print(os.path.dirname(path))
अलग-अलग Path Parts को सही तरीके से जोड़ने के लिए os.path.join() उपयोगी है।
import os
path = os.path.join("Documents", "Python", "test.txt")
print(path)
Windows पर यह सामान्यतः इस तरह दिखाई दे सकता है:
Documents\Python\test.txt
Linux/macOS पर Separator अलग होगा।
किसी Relative Path को Absolute Path में बदलने के लिए।
import os
print(os.path.abspath("test.txt"))
Operating System के Environment Variables तक पहुँचने के लिए os.environ का उपयोग किया जा सकता है।
import os
print(os.environ.get("PATH"))
किसी Variable के उपलब्ध न होने पर None मिल सकता है।
os.name Operating System से संबंधित एक Basic Identifier देता है।
import os
print(os.name)
Windows पर सामान्यतः:
nt
Linux/macOS पर सामान्यतः:
posix
os.system() Operating System Shell Command चलाने के लिए उपयोग किया जा सकता है।
उदाहरण:
import os
os.system("echo Hello")
हालाँकि अधिक Complex और सुरक्षित Process Management के लिए Python का subprocess Module अक्सर बेहतर विकल्प होता है।
Practical Program 1 – Current Directory
import os
print("Current Directory:")
print(os.getcwd())
Practical Program 2 – Files और Folders की List
import os
items = os.listdir()
for item in items:
print(item)
Practical Program 3 – Folder बनाना
import os
folder = "PythonNotes"
if not os.path.exists(folder):
os.mkdir(folder)
print("Folder Created")
else:
print("Folder already exists")
Practical Program 4 – File Exists Check
import os
file = "notes.txt"
if os.path.exists(file):
print("File Found")
else:
print("File Not Found")
Practical Program 5 – File Rename
import os
old_name = "old.txt"
new_name = "new.txt"
if os.path.exists(old_name):
os.rename(old_name, new_name)
print("File Renamed")
else:
print("File Not Found")
Practical Program 6 – File Delete
import os
file = "test.txt"
if os.path.isfile(file):
os.remove(file)
print("File Deleted")
else:
print("File Not Found")
Practical Program 7 – Folder की Files गिनना
import os
items = os.listdir()
count = 0
for item in items:
if os.path.isfile(item):
count += 1
print("Total Files:", count)
Practical Program 8 – केवल Folders की List
import os
items = os.listdir()
for item in items:
if os.path.isdir(item):
print(item)
Practical Program 9 – File Size
import os
file = "test.txt"
if os.path.isfile(file):
size = os.path.getsize(file)
print("File Size:", size, "Bytes")
Practical Program 10 – Path Join
import os
folder = "Documents"
file = "notes.txt"
path = os.path.join(folder, file)
print(path)
os.walk() किसी Directory के अंदर मौजूद सभी Subdirectories और Files को Traverse करने के लिए उपयोगी है।
import os
for root, dirs, files in os.walk("."):
print("Folder:", root)
for file in files:
print("File:", file)
यह बड़े Folder Structure को Explore करने में उपयोगी है।
os.path Path से संबंधित Functions का एक महत्वपूर्ण भाग है।
कुछ प्रमुख Functions—
exists()
isfile()
isdir()
getsize()
basename()
dirname()
join()
abspath()
महत्वपूर्ण Functions एक नजर में
|
Function |
उपयोग |
|
os.getcwd() |
Current Directory |
|
os.chdir() |
Directory बदलना |
|
os.listdir() |
Files/Folders की List |
|
os.mkdir() |
Directory बनाना |
|
os.makedirs() |
Nested Directories बनाना |
|
os.rename() |
Rename करना |
|
os.remove() |
File Delete |
|
os.rmdir() |
Empty Directory Delete |
|
os.path.exists() |
Path मौजूद है या नहीं |
|
os.path.isfile() |
File Check |
|
os.path.isdir() |
Directory Check |
|
os.path.getsize() |
File Size |
|
os.path.basename() |
अंतिम नाम |
|
os.path.dirname() |
Directory Path |
|
os.path.join() |
Path जोड़ना |
|
os.path.abspath() |
Absolute Path |
|
os.walk() |
Directory Tree Traverse |
Modern Python में File और Path Operations के लिए pathlib भी बहुत उपयोगी और Readable विकल्प है।
उदाहरण:
from pathlib import Path
path = Path("test.txt")
print(path.exists())
फिर भी os Module Python Fundamentals और पुराने/मौजूदा Codebases को समझने के लिए बहुत महत्वपूर्ण है।
File और Folder Delete करने वाले Functions का उपयोग करते समय हमेशा Path Check करें।
उदाहरण:
if os.path.isfile("test.txt"):
os.remove("test.txt")
बिना जाँच के Delete Operation करने से गलत File हट सकती है या Error आ सकता है।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- os का संबंध Operating System से है।
- os.getcwd() Current Working Directory बताता है।
- os.chdir() Current Directory बदलता है।
- os.listdir() Directory की Contents की List देता है।
- os.mkdir() Directory बनाता है।
- os.makedirs() Nested Directories बना सकता है।
- os.rename() File या Directory का नाम बदलता है।
- os.remove() File Delete करता है।
- os.rmdir() Empty Directory Delete करता है।
- os.path.exists() Path के अस्तित्व की जाँच करता है।
- os.path.isfile() File की जाँच करता है।
- os.path.isdir() Directory की जाँच करता है।
- os.path.join() Path Parts को जोड़ता है।
- os.walk() Directory Tree को Traverse करता है।
Frequently Asked Questions (FAQs)
प्रश्न 1. Python में Folder बनाने के लिए कौन-सा Function है?
os.mkdir()
प्रश्न 2. File Delete करने के लिए कौन-सा Function है?
os.remove()
प्रश्न 3. Folder Delete करने के लिए कौन-सा Function है?
os.rmdir()
यह सामान्यतः Empty Directory के लिए उपयोग होता है।
प्रश्न 4. Current Directory कैसे पता करें?
os.getcwd()
प्रश्न 5. File मौजूद है या नहीं कैसे Check करें?
os.path.exists()
प्रश्न 6. File और Folder में अंतर कैसे Check करें?
os.path.isfile()
os.path.isdir()
- os → Operating System से Interaction
- getcwd() → Current Directory
- chdir() → Directory Change
- listdir() → Directory Contents
- mkdir() → Folder Create
- makedirs() → Nested Folders Create
- rename() → Rename
- remove() → File Delete
- rmdir() → Empty Folder Delete
- exists() → Path Exists Check
- isfile() → File Check
- isdir() → Directory Check
- join() → Path Join
- walk() → Folder Tree Traverse
अगला अध्याय —Python File Handling
इसमें File क्या है, File Modes (r, w, a, x, b), open(), read(), readline(), readlines(), write(), writelines(), close(), with open(), Text/Binary Files और Practical Programs विस्तार से होंगे।