Serdia
Serdia

Reputation: 4428

How to modify python code to move converted files to a separate folder?

I have python code that converts multiple .xlsx files to .csv. But it puts them into the same folder. How to modify this code to make sure it puts .csv files to a separate folder?

import pandas as pd
import glob
excel_files = glob.glob('C:/Users/username/Documents/TestFolder/JanuaryDataSentToResourcePro/*.xlsx') # assume the path
for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'ResourceProDailyDataset') # if the sheet name is always the same.
    df.to_csv(out) 

Upvotes: 0

Views: 111

Answers (3)

abybaddi009
abybaddi009

Reputation: 1104

First split the path, remove the folder that you don't want and replace it with some other folder.

import pandas as pd
import os
import glob
excel_files = glob.glob('C:/Users/username/Documents/TestFolder/JanuaryDataSentToResourcePro/*.xlsx') # assume the path
# folder name for the converted csv files
different_folder = "csv_folder"
for excel in excel_files:
    # make a csv path
    csv_folder_path =  "\\".join(excel.split('\\')[:-1])+"\\"+different_folder+"\\"
    if not os.path.exists(csv_folder_path):
        # create it if it doesn't exist
        os.makedirs(csv_folder_path)
    # full path of the csv file
    out = csv_folder_path+excel.split('\\')[-1].split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'ResourceProDailyDataset') # if the sheet name is always the same.
    df.to_csv(out) 

This will create a new folder csv_folder in all the folders that have Excel file in them and the converted csv files would be placed in this folder.

Upvotes: 1

r.ook
r.ook

Reputation: 13898

Split the directory from your file name and then give your out a new directory :

for excel in excel_files:
    folder = r'C:\ASeparateFolder\'
    out = folder + excel.split('\\')[-1].split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'ResourceProDailyDataset') # if the sheet name is always the same.
    df.to_csv(out)

Note I used '\\' to split, it seems to be the common splitting point for glob whether you use '\' or '/' for your initial excel_file directory.

Upvotes: 1

Burgan
Burgan

Reputation: 900

You can change the variable that controls your output file. Try something like this

out = 'some_directory/' + excel.split('.')[0]+ '.csv'

The directory will have to exist for this to work.

Upvotes: 0

Related Questions