jeudi 25 février 2021

how to copy all csv files from different source folders into target as per source folders using python?

I have code as below to copy all *.csv files from different source folders, but i want to copy in different folders as per source, i could able to copy all csv files to single folder but i want it into different folders as source folders.

import os
import shutil
import fnmatch

dir = 'C:\\data\\projects\\'
patterns = ['project1','project2']
dest = 'D:\\data\\projects\\'

for root, dirs, files in os.walk(dir):

    for pattern in patterns:

        for filename in fnmatch.filter(files, '*.csv'):
            source = (os.path.join(root, filename))
            print(source)
            shutil.copy(source, dest)

Aucun commentaire:

Enregistrer un commentaire