import os
import shutil
import glob
import pandas as pd
path = '/mypath/'
# rename files
count = 1
for root, dirs, files in os.walk(path):
for i in files:
if i == 'whatever.csv':
os.rename(os.path.join(root, i), os.path.join(root, "whatever" + str(count) + ".csv"))
count += 1
# delete unwanted files
main_dir = path
folders = os.listdir(main_dir)
for (dirname, dirs, files) in os.walk(main_dir):
for file in files:
if file.startswith('dontwant'):
source_file = os.path.join(dirname, file)
os.remove(source_file)
# copy files to dir
for root, dirs, files in os.walk(path): # replace the . with your starting directory
for file in files:
if file.endswith('.csv'):
path_file = os.path.join(root,file)
shutil.copy2(path_file,path) # change you destination dir
# combine files
os.chdir(path)
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')