0

Im trying to create a small script that aims to merge all the file .csv in a single csv file in Python. After merging all the .csv files in a single one the script converts the file into an xlsx format. The problem of my script is that it works for a specific folder, is possible to generalize that? for example add a variable that takes the string of the current folder path and then so on... This is the code:

import os
import glob
import pandas as pd
#from openpyxl.workbook import Workbook,load_workbook
from openpyxl import Workbook, load_workbook

os.chdir('C:\\Users\\f\\Desktop\\TestPython') #2 parenthesis after C
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv\\
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')



# reading the csv file
cvsDataframe = pd.read_csv(r'C:\Users\f\Desktop\TestPython\combined_csv.csv')

# creating an output excel file
resultExcelFile = pd.ExcelWriter(r'C:\Users\f\Desktop\TestPython\ResultExcelFile.xlsx')

# converting the csv file to an excel file
cvsDataframe.to_excel(resultExcelFile, index=False)

i tried to merge all the csv files in a single file, then convert the file into an xlsx format

Andrea
  • 13
  • 4

0 Answers0