path = "C:\\Users\\amit_\\Desktop\\MyProject\\"
उपरोक्त पथ से .csv एक्सटेंशन वाली फ़ाइलें पढ़ें:
filenames = glob.glob(path + "\*.csv")
आइए अब सभी CSV फ़ाइलों को इटरेट करने, पढ़ने और प्रिंट करने के लिए एक फॉर लूप लिखें:
for file in filenames:
# CSV फ़ाइलें पढ़ें
print("\nReading file = ", file)
print(pd.read_csv(file))
उदाहरण
निम्नलिखित पूरा कोड है:
import pandas as pd
import glob
# MyProject फ़ोल्डर से CSV फ़ाइलें प्राप्त करें
path = "C:\\Users\\amit_\\Desktop\\MyProject\\"
# एक्सटेंशन .csv वाली सभी फ़ाइलें पढ़ें
filenames = glob.glob(path + "\*.csv")
print('File names:', filenames)
# सभी CSV फ़ाइलों को इटरेट करने के लिए फॉर लूप
for file in filenames:
# CSV फ़ाइलें पढ़ें
print("\nReading file = ", file)
print(pd.read_csv(file))
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
File names:['C:\\Users\\amit_\\Desktop\\MyProject\\Sales1.xlsx', 'C:\\Users\\amit_\\Desktop\\MyProject\\Sales2.xlsx'] Reading file = C:\Users\amit_\Desktop\MyProject\Sales1.xlsx Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 Reading file = C:\Users\amit_\Desktop\MyProject\Sales2.xlsx Car Place UnitsSold 0 BMW Delhi 95 1 Mercedes Hyderabad 80 2 Lamborgini Chandigarh 80