Home > Net >  How to iterate through subfolders and convert files into csv while saving them in that subfolder
How to iterate through subfolders and convert files into csv while saving them in that subfolder

Time:06-10

I have a folder and subdirectories which have txt files in it. I want to convert each txt file to csv and save the files with the same name as txt file but in csv format. For instance, I have a folder called A and A has

  • subfolder B (B has subfolders as C,D,etc.). Each subfolder has a file in it with different names such as test1.txt, test112.json, etc.
  • subfolder E (E has subfolders as F,K,etc.). Each subfolder has a file in it with different names such as sub folder F has testF.txt, testFgf.json, etc. in it, sub folder K has testKk.txt.

I would like to convert each .txt file into .csv for each sub folder and have the same file name as:

  • subfolder B (B has subfolders as C,D,etc.). Each subfolder has a file in it with different names such as test1.txt, test1.json, test1.csv.
  • subfolder E (E has subfolders as F,K,etc.). Each subfolder has a file in it with different names such as sub folder F has testF.txt, testF.json, testF.csv , sub folder K has testKk.txt, testKk.csv. I have tried:
root="C:/user/main_folder/A/"
for path, subdirs, files in os.walk(root):
    for name in files:
        filepath = root   os.sep   name
         if filepath.endswith(".txt"):
            print(os.path.join(path, name))
            file=task_info
            task_info.to_csv(path.join(filepath, file_name), index=False)

In here, I can get the right file path from each sub folders but I cannot convert them into csv and save them in the right folder with the right names. Any help would be appreciated.

CodePudding user response:

you can use recursion

root="C:/user/main_folder/A"

def explore_and_execute(initial_path):
    for item in os.listdir(initial_path):
        item_fullpath = initial_path "/" item
        if os.path.isdir(item):
            explore_and_execute(item_fullpath)
        if os.path.isfile(item):
            if item.endswith(".txt"):
                os.rename(item_fullpath, item_fullpath[:-4] ".csv")

explore_and_execute(root)    

here you will be looping until the end of the directory branch

CodePudding user response:

With the path to the file you will need to use:

def store(data, root, name):
    name = name.split('.')[0] # To get just the name and remove the file extension
    with open(root os.sep name '.csv'. 'w') as f2:
       f2.write(data)  

root="C:/user/main_folder/A/"
for path, subdirs, files in os.walk(root):
    for name in files:
        filepath = root   os.sep   name
            if filepath.endswith(".txt"):
                with open('filepath', 'r') as file:
                    # Here you can do anything you want to do with the file if you want to modify it somehow and if it is a .txt with .csv format, do nothing.
                    store(file, root, name)
  • Related