简体   繁体   中英

Batch extract and rename csv files from multiple folders - Python

I have a 350 folders in a directory that all contain two csv files named "certificates" and "recommendations". I want to move every single "certificates.csv" files into a new folder but also change the name of those .csv files in the process, adding an index number as a suffix so they do not overwrite each other ie "certificates1", "certificates2"...."certificates350" etc.

I managed to get to this when trying to move all the files into one folder:

import os
import shutil
src_dir = "C:/IN"
dst_dir = "C:/OUT"
for root, dirs, files in os.walk(src_dir):
    for f in files:
        if f.endswith('.csv'):
            shutil.copy(os.path.join(root,f), dst_dir)

But all that does is cycle through the files overwriting each other in the process as they all have the same names - "certificates.csv"

Just check if the file exists and increment a counter until filename is available:

for root, dirs, files in os.walk(src_dir):
    for f in files:
        if f.endswith('.csv'):
            index = 0
            while True:        
                new_path = os.path.join(dst_dir, str(index) + f)
                if os.path.isfile(new_path):
                    index += 1
                    continue

                shutil.copy(os.path.join(root, f), new_path)
                break

If you want a suffix, not a prefix for a file, you should split name from extension, append a number, then join again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM