2

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)
Akhil Mathew
  • 1,571
  • 3
  • 34
  • 69
Justin
  • 125
  • 2
  • 8

3 Answers3

4

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)
Błotosmętek
  • 12,717
  • 19
  • 29
2

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)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
0

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?

ravindar
  • 406
  • 4
  • 13