Created
January 7, 2020 16:12
-
-
Save achinta/bf0c64a2ffc343d9d2862baf08181892 to your computer and use it in GitHub Desktop.
Copy downloaded pdfs(and other docs) to a single folder after coursera-dl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run this in the root of the downloaded folder. It will copy the pdf files from sub folders to base folder | |
""" | |
import os | |
from glob import glob | |
import shutil | |
cwd = os.getcwd() | |
if not os.path.isdir('renamed'): | |
os.mkdir('renamed') | |
filetypes = ['pdf','pptx','ppt'] | |
for root, dirs, files in os.walk(cwd): | |
for file in files: | |
if file.split('.')[-1] not in filetypes: | |
continue | |
path = '/'.join([root, file]) | |
paths = path.split('/') | |
new_filename = '_'.join(paths[-3:]) | |
base = '/'.join(paths[:-3]) | |
new_dir = '/'.join([base,'renamed']) | |
new_path = '/'.join([new_dir, new_filename]) | |
for dir_name in os.listdir('.'): | |
if dir_name == 'renamed': | |
continue | |
if os.path.isdir(dir_name): | |
shutil.rmtree(dir_name) | |
os.rmdir('renamed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment