-
-
Save holidayz1/fe68948fd28d408e99c837c0b7942b7c to your computer and use it in GitHub Desktop.
Script organizing files and dividing them into folders according to their type
This file contains hidden or 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
| #! python3 | |
| import os | |
| import shutil | |
| from pathlib import Path | |
| path = Path('~/folder/foo') | |
| absolute_path = path.absolute() | |
| home_path = str(os.getcwd())+"\Downloads\\" | |
| imagepath=str(os.path.join(os.path.dirname(os.path.realpath(__file__)),'Images')) | |
| Codespath=str(os.path.join(os.path.dirname(os.path.realpath(__file__)),'Codes')) | |
| Docspath=str(os.path.join(os.path.dirname(os.path.realpath(__file__)),'Docs')) | |
| Archivespath=str(os.path.join(os.path.dirname(os.path.realpath(__file__)),'Archives')) | |
| print(str(imagepath)) | |
| print(str(Codespath)) | |
| print(str(Docspath)) | |
| print(str(Archivespath)) | |
| print("\t [+] Welcome in organize.py script - happy clean folder ;) \n") | |
| images = 0 | |
| Code = 0 | |
| Docs = 0 | |
| archive = 0 | |
| files = os.listdir(home_path) # Get all the files in that directory | |
| print("Files in %r: %s \n" % (home_path, files)) | |
| for file in os.listdir(home_path): | |
| # organize images into Images folder | |
| if file.endswith(("png", "jpg", "git", "jpeg")): | |
| if not os.path.exists(imagepath): | |
| os.mkdir(imagepath) | |
| shutil.move(os.path.join(home_path, file), imagepath) | |
| images = images + 1 | |
| # organize Code file into Codes folder | |
| elif file.endswith(("py", "java", "php", "js", "exe", "css", "sh", "bash", "html","msi")): | |
| if file != "organize.py": | |
| if not os.path.exists(Codespath): | |
| os.mkdir(Codespath) | |
| shutil.move(os.path.join(home_path, file), Codespath) | |
| Code = Code + 1 | |
| # organize Docs file into Documents folder | |
| elif file.endswith(("pdf", "txt", "ppt", "docx", "pptx","doc","epub")): | |
| if not os.path.exists(Docspath): | |
| os.mkdir(Docspath) | |
| shutil.move(os.path.join(home_path, file), Docspath) | |
| Docs = Docs + 1 | |
| # organize archive file into archives folder | |
| elif file.endswith(("zip", "rar")): | |
| if not os.path.exists(Archivespath): | |
| os.mkdir(Archivespath) | |
| shutil.move(os.path.join(home_path, file), Archivespath) | |
| archive = archive + 1 | |
| print("[", images, "] Images organized Done!") | |
| print("[", Code, "] Codes organized Done!") | |
| print("[", Docs, "] Docs organized Done!") | |
| print("[", archive, "] Archives organized Done!") | |
| print("\n") | |
| print(" Your files were successfully organized ;)") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated paths so it can run anywhere.