डायरेक्टरी में सभी एक्सेल फ़ाइलों को पढ़ने के लिए, Glob मॉड्यूल और read_excel() मेथड का उपयोग करें।
मान लीजिए निम्नलिखित एक डायरेक्टरी में हमारी एक्सेल फ़ाइलें हैं:
Sales1.xlsx

Sales2.xlsx

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