I'm looking to understand if my code has an obvious blockage or performance pain point that will cause it to operate slower or use more memory than it should.
The current Excelfile i am processing with this script has 5 sheets. the first sheet is the largest containing 6000 rows. I've never done this before so am unsure how long this should really take.
import pandas as pd
import glob
import os
"""
Retrieves csv files from folders and appends them to an existing Excel sheet.
Reference
https://www.geeksforgeeks.org/how-to-append-data-in-excel-using-python/
https://www.geeksforgeeks.org/how-to-merge-multiple-csv-files-into-a-single-pandas-dataframe/
https://stackoverflow.com/questions/26521266/using-pandas-to-pd-read-excel-for-multiple-worksheets-of-the-same-workbook
"""
dirs = []
searchFiles = []
files = []
mainFile = os.path.join(os.getcwd(), "Phone Performance.xlsx")
xls = pd.ExcelFile("Phone Performance.xlsx")
sheetNames = xls.sheet_names
dirfiles = os.listdir(os.getcwd())
# creating directories to search
for file in dirfiles:
if os.path.isdir(file) and not file.startswith("."):
dirs.append(file + "\*.csv")
# yield the files to be processed
def getPath(directories):
for item in directories:
path = os.path.join(os.getcwd(), item)
files.append(glob.glob(path))
yield files
# use concat to process data files if there is single or multiple in a directory
def getData(directories):
for file in getPath(dirs):
for item in file:
data = pd.concat(
filter(lambda x: not x.empty, map(pd.read_csv, item)), ignore_index=True
)
yield data
# append new data to existing data for each sheet in the Excelfile
def existingData(file, name):
count = 0
while count <= len(name):
existingData = pd.read_excel(file, name[count])
yield existingData._append(next(getData(dirs)), ignore_index=True)
count += 1
data = next(existingData(xls, sheetNames))
for sheet in sheetNames:
with pd.ExcelWriter(xls, mode="a", if_sheet_exists="overlay") as writer:
data.to_excel(xls, sheet, index=False)
# removing used files
for dir in dirs:
if os.path.isfile(file):
os.remove(file)
Is there something I can improve to make my process more performant?