Skip to content

Instantly share code, notes, and snippets.

@Vuong-Chu
Created October 21, 2023 21:46
Show Gist options
  • Save Vuong-Chu/d0e95ffef22106edc455050e6953c929 to your computer and use it in GitHub Desktop.
Save Vuong-Chu/d0e95ffef22106edc455050e6953c929 to your computer and use it in GitHub Desktop.
Copy multiple files from sub-folders
'''
This file is used to loop through sub-directories
and copy multiple files with a specific filename,
then rename them in a new folder.
'''
import os, shutil, re
def main():
src = "/media/Folder From"
dst = "/media/Folder To"
regex = re.compile("(A.*Z.csv)")
i = 0
for root, dirs, files in os.walk(src):
for file in files:
if regex.match(file):
#base, extension = os.path.splitext(file)
#new_name = os.path.join(dst, base + "_" + str(i) + extension)
new_name = os.path.join(dst, str(i) + ".csv")
print(new_name)
shutil.copy(os.path.join(root, file), new_name)
i+=1
print("Done!")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment