简体   繁体   中英

Copy all .csv files in a directory of folders to one folder in python

I am trying to copy all .csv files within a parent folder and all sub-folders within, to a new destination ("C:/Projects/CSVFiles").

I have used the following code(from elsewhere on the forum) but this only copies the .csv files in the parent directory (DataFiles) and not from the sub-folders within /datafiles/.

Any advice appreciated. Thanks

import glob
import shutil
import os

src_dir = "C:/Projects/DataFiles"
dst_dir = "C:/Projects/CSVFiles"
for CSVfile in glob.iglob(os.path.join(src_dir, "*.csv")):
shutil.copy(Excelfile, dst_dir)

Use os.walk to traverse the directory tree.

import os
import shutil
src_dir = "C:/Projects/DataFiles"
dst_dir = "C:/Projects/CSVFiles"
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)

Starting from python 3.5, glob supports the recursive parameter :

glob.iglob(os.path.join(src_dir, "**", "*.csv"), recursive=True)

In older python versions you can use os.walk instead:

import os

for root, dirs, files in os.walk(src_dir):
    for filename in files:
        if not filename.endswith('.csv'):
            continue

        filepath = os.path.join(root, filename)

Python 2.2 to 3.4

import fnmatch
import os

src_dir = "C:/Projects/DataFiles"
dst_dir = "C:/Projects/CSVFiles"

for root, dirnames, filenames in os.walk(src_dir):
    for filename in fnmatch.filter(filenames, '*.csv'):
        shutil.copy(os.path.join(root, filename),dst_dir)

Ref: Use a Glob() to find files recursively in Python?

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