5

I would like to read several input files from a folder, perform some transformations,create folders on the fly and write the csv to corresponding folders. The point here is I have the input path which is like

"Input files\P1_set1\Set1_Folder_1_File_1_Hour09.csv" - for a single patient (This file contains readings of patient (P1) at 9th hour)

Similarly, there are multiple files for each patient and each patient files are grouped under each folder as shown below

enter image description here

So, to read each file, I am using wildcard regex as shown below in code

I have already tried using the glob package and am able to read it successfully but am facing issue while creating the output folders and saving the files. I am parsing the file string as shown below

f = "Input files\P1_set1\Set1_Folder_1_File_1_Hour09.csv"

f[12:] = "P1_set1\Set1_Folder_1_File_1_Hour09.csv"

filenames = sorted(glob.glob('Input files\P*_set1\*.csv'))
for f in filenames:
   print(f)     #This will print the full path
   print(f[12:]) # This print the folder structure along with filename
   df_transform = pd.read_csv(f)
   df_transform = df_transform.drop(['Format 10','Time','Hour'],axis=1)
   df_transform.to_csv("Output\" + str(f[12:]),index=False)

I expect the output folder to have the csv files which are grouped by each patient under their respective folders. The screenshot below shows how the transformed files should be arranged in output folder (same structure as input folder). Please note that "Output" folder is already existing (it's easy to create one folder you know) enter image description here

Community
  • 1
  • 1
The Great
  • 7,215
  • 7
  • 40
  • 128

1 Answers1

3

So to read files in a folder use os library then you can do

import os
folder_path = "path_to_your_folder"
dir = os.listdir(folder_path)
for x in dir:
    df_transform = pd.read_csv(f)
    df_transform = df_transform.drop(['Format 10','Time','Hour'],axis=1)
    if os.path.isdir("/home/el"):
        df_transform.to_csv("Output/" + str(f[12:]),index=False)
    else:
        os.makedirs(folder_path+"/")
        df_transform.to_csv("Output/" + str(f[12:]),index=False)    

Now instead of user f[12:] split the x in for loop like

file_name = x.split('/')[-1] #if you want filename.csv

Let me know if this is what you wanted

Bhawesh Chandola
  • 511
  • 5
  • 19
  • When glob allowes me to fetch files from different directories based on wildcard match, I can just make us of os.makedirs() function while writing the csv file by passing the f[12:] as argument. would you recommend thatr? – The Great Apr 04 '19 at 05:49
  • Yes you can just use os.makedirs to mke directory with glob if you are sure that the file name will exists after 12th index – Bhawesh Chandola Apr 04 '19 at 05:52
  • @ Bhawesh Chandola - Can you please help me with the below issue? t = os.makedirs("Output\\" + str(f[12:20])) # f[12:20] gives P1_set1,P2_set1 etc df_transform.to_csv(str(t)+"\" + str(f[21:]),index=False). I am able to create the output directory with each patient folder successfully but unable to write csv in that folders. – The Great Apr 04 '19 at 06:36
  • Am getting a syntax error that "EOL while scanning string literal" even though the parenthesis are balanced . The below given is the piece of code causing issue df_transform.to_csv(str(t) + "\" + str(f[21:]),index=False) – The Great Apr 04 '19 at 06:40
  • @SELVA os.makedirs doesn't return the path of the cerated folder just save the directory of the folder to be created in a variable and use in to_csv like x = "Output\\"+str(f[12:20]) os.makedirs(x) df_transform.to_csv(x+"\" + str(f[21:]),index=False) – Bhawesh Chandola Apr 04 '19 at 06:41
  • Are you sure the last stemament is correct? I mean this one ""df_transform.to_csv(x+"\" + str(f[21:]),index=False)"" usually 'False' keyword changes color to green but in this case this doesn't even though parenthesis is balanced – The Great Apr 04 '19 at 06:49
  • 1
    @SELVA this is due to single '\' change your statement to df_transform.to_csv(x+"\\" + str(f[21:]),index=False) – Bhawesh Chandola Apr 04 '19 at 06:52
  • 1
    You are awesome – The Great Apr 04 '19 at 06:56
  • just helping a fellow dev. :) – Bhawesh Chandola Apr 04 '19 at 06:57