Data Science · मराठी आवृत्ती · Python basics
P9.6 Python मधले नेहमीचे File Errors आणि उपाय
हे का शिकायचं?
File नसणं, चुकीचं नाव, चुकीचा encoding — हे टाळता येतात.
हे काय आहे?
FileNotFoundError म्हणजे path चुकला. UnicodeDecodeError म्हणजे encoding चुकला. Path.exists() ने आधी तपासता येतं.
कसं करायचं?
Step 1. Path.exists() ने तपासा.
Step 2. try/except FileNotFoundError वापरा.
Step 3. encoding="utf-8" द्या.
उदाहरणं: copy करा आणि चालवा
उदाहरण A: Missing file हाताळा
हा code चालवा:
try:
with open("april.csv", encoding="utf-8") as f:
print(f.read())
except FileNotFoundError:
print("april.csv not found - export it first")
तुम्हाला दिसणारा output:
april.csv not found - export it first
याचा अर्थ: File नसताना program न थांबता स्पष्ट message मिळाला.