python copy files by wildcards

63,539

Solution 1

import glob
import shutil
dest_dir = "C:/test"
for file in glob.glob(r'C:/*.txt'):
    print(file)
    shutil.copy(file, dest_dir)

Solution 2

Use glob.glob() to get a list of the matching filenames and then iterate over the list.

Share:
63,539
Admin
Author by

Admin

Updated on March 15, 2020

Comments

  • Admin
    Admin about 4 years

    I am learning python (python 3) and I can copy 1 file to a new directory by doing this

    import shutil 
    shutil.copyfile('C:/test/test.txt', 'C:/lol/test.txt')
    

    What I am now trying to do is to copy all *.txt files from C:/ to C:/test

    *.txt is a wildcard to search for all the text files on my hard drive