Skip to content

Instantly share code, notes, and snippets.

@estasney
Created July 29, 2019 19:14
Show Gist options
  • Save estasney/dcc1e8eaa8ad6416bf8fe864f97d433f to your computer and use it in GitHub Desktop.
Save estasney/dcc1e8eaa8ad6416bf8fe864f97d433f to your computer and use it in GitHub Desktop.
import pandas as pd
import os
SPREADSHEET_FOLDER = r"C:\Users" # Which folder are these files located in?
OUTPUT_FILEPATH = r"C:\Users" # Where should it go?
if not os.path.isdir(SPREADSHEET_FOLDER):
raise NotADirectoryError
if '.xls' not in OUTPUT_FILEPATH and '.csv' not in OUTPUT_FILEPATH:
raise Exception("Output path must have ext of .csv, .xls, or .xlsx")
frames = []
for spreadsheet in os.listdir(SPREADSHEET_FOLDER):
name, file_ext = os.path.splitext(spreadsheet)
if file_ext == '.csv':
open_method = pd.read_csv
elif '.xls' in file_ext:
open_method = pd.read_excel
else:
print("File {} is not a spreadsheet, skipping".format(spreadsheet))
continue
full_path = os.path.join(SPREADSHEET_FOLDER, spreadsheet)
df = open_method(full_path)
frames.append(df)
df = pd.concat(frames)
if '.xls' in OUTPUT_FILEPATH:
save_method = df.to_excel
else:
save_method = df.to_csv
save_method(OUTPUT_FILEPATH, index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment