Skip to content

Instantly share code, notes, and snippets.

@goks
Last active March 19, 2017 18:56
Show Gist options
  • Save goks/3124a2bad4d0ec1197b3dd83bf85cdc1 to your computer and use it in GitHub Desktop.
Save goks/3124a2bad4d0ec1197b3dd83bf85cdc1 to your computer and use it in GitHub Desktop.
Copy all the files from subdirectories to another directory without the folder hierarchy.
import os, sys
from shutil import copy
import progressbar
read_des = "./"
write_des = "../final/"
count = 0
max_count = 0
for root, dirs, files in os.walk(read_des):
max_count += len(files)
max_count-=1
bar = progressbar.ProgressBar(maxval=max_count, widgets=[progressbar.Bar('=', 'Working: [', ']'), ' ', progressbar.Percentage()])
bar.start()
try:
os.mkdir(write_des)
except:
print("Write_des already exists.")
for folder in os.listdir(read_des):
if not os.path.isdir(folder):
continue
path = folder + '/'
try:
for file in os.listdir(path):
file_path = path + file
copy(file_path, write_des)
count+=1
bar.update(count)
except Exception as e:
raise e
print("DONE...")
print(str(count) + " out of " + str(max_count) + " files processed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment