किसी निर्देशिका में सभी एक्सेल फ़ाइलों को पढ़ने के लिए, ग्लोब मॉड्यूल और read_excel() विधि का उपयोग करें।
मान लें कि एक निर्देशिका में हमारी एक्सेल फाइलें निम्नलिखित हैं -
बिक्री1.xlsx

बिक्री2.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
# getting excel files from Directory Desktop
path = "C:\\Users\\amit_\\Desktop\\"
# read all the files with extension .xlsx i.e. excel
filenames = glob.glob(path + "\*.xlsx")
print('File names:', filenames)
# for loop to iterate all excel files
for file in filenames:
# reading excel files
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