Data Science · मराठी आवृत्ती · Python basics
P9.1 Python मध्ये File Paths आणि Working Folder
हे का शिकायचं?
"File सापडत नाही" ही सगळ्यात नेहमीची अडचण. Python कुठे शोधतो ते माहीत हवं.
हे काय आहे?
Working folder म्हणजे जिथून Python files शोधतो. os.getcwd() तो दाखवतो. pathlib.Path ने Windows आणि Mac दोन्हीवर चालणारा path बनतो.
कसं करायचं?
Step 1. os.getcwd() ने folder पाहा.
Step 2. Path("data") / "file.csv" ने path बनवा.
उदाहरणं: copy करा आणि चालवा
उदाहरण A: मी कुठे आहे?
आधी त्याच folder मध्ये sales.csv ही input file तयार करा:
date,store,item,qty,price
2026-03-01,Pune,Milk,3,56
2026-03-01,Mumbai,Bread,2,40
2026-03-02,Pune,Eggs,1,84
2026-03-02,Nagpur,Milk,5,56
हा code चालवा:
from pathlib import Path
p = Path("sales.csv")
print(p.exists())
print(p.suffix)
तुम्हाला दिसणारा output:
True
.csv
याचा अर्थ: exists() ने file working folder मध्ये आहे का ते तपासलं (True) आणि suffix ने extension (.csv) मिळालं.
उदाहरण B: सुरक्षितपणे path बनवा
हा code चालवा:
from pathlib import Path
p = Path("reports") / "2026" / "march.csv"
print(p.as_posix())
print(p.name, p.stem)
तुम्हाला दिसणारा output:
reports/2026/march.csv
march.csv march
याचा अर्थ: / ने path जोडला, त्यामुळे slash ची काळजी नको.