|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import sys |
| 4 | + |
| 5 | +dir_path = os.path.dirname(os.path.realpath(__file__)) |
| 6 | + |
| 7 | + |
| 8 | +def organizeThis(dir_path): |
| 9 | + try: |
| 10 | + print("Organising your files...") |
| 11 | + for filename in os.listdir(dir_path): |
| 12 | + # Check if files are images |
| 13 | + if filename.lower().endswith( |
| 14 | + (".png", ".jpg", ".jpeg", ".gif", ".bmp", ".pbm", ".pnm") |
| 15 | + ): |
| 16 | + # If images folder doesnt exist then create |
| 17 | + if not os.path.exists(dir_path + "/images"): |
| 18 | + os.makedirs(dir_path + "/images") |
| 19 | + shutil.copy2(dir_path + "/" + filename, dir_path + "/images") |
| 20 | + os.remove(dir_path + "/" + filename) |
| 21 | + |
| 22 | + # Check if files are music |
| 23 | + if filename.lower().endswith( |
| 24 | + (".wav", ".mp3", ".flac", ".3gp", ".aa", ".aax", ".aiff", ".raw") |
| 25 | + ): |
| 26 | + # If music folder doesnt exist then create |
| 27 | + if not os.path.exists(dir_path + "music"): |
| 28 | + os.makedirs(dir_path + "/music") |
| 29 | + shutil.copy2(dir_path + "/" + filename, dir_path + "/music") |
| 30 | + os.remove(dir_path + "/" + filename) |
| 31 | + |
| 32 | + # Check if files are videos |
| 33 | + if filename.lower().endswith((".mp4", ".mov", ".webm")): |
| 34 | + # If videos folder doesnt exist then create |
| 35 | + if not os.path.exists(dir_path + "/videos"): |
| 36 | + os.makedirs(dir_path + "/videos") |
| 37 | + shutil.copy2(dir_path + "/" + filename, dir_path + "/videos") |
| 38 | + os.remove(dir_path + "/" + filename) |
| 39 | + |
| 40 | + # Check if files are executables |
| 41 | + if filename.lower().endswith((".exe", ".tar", ".deb")): |
| 42 | + # If executables folder doesnt exist then create |
| 43 | + if not os.path.exists(dir_path + "/executables"): |
| 44 | + os.makedirs(dir_path + "/executables") |
| 45 | + shutil.copy2(dir_path + "/" + filename, dir_path + "/executables") |
| 46 | + os.remove(dir_path + "/" + filename) |
| 47 | + |
| 48 | + # Check if files are documents |
| 49 | + if filename.lower().endswith( |
| 50 | + (".txt", ".pdf", ".docx", ".csv", ".xlsx", ".pptx") |
| 51 | + ): |
| 52 | + # If documents folder doesnt exist then create |
| 53 | + if not os.path.exists(dir_path + "/documents"): |
| 54 | + os.makedirs(dir_path + "/documents") |
| 55 | + shutil.copy2(dir_path + "/" + filename, dir_path + "/documents") |
| 56 | + os.remove(dir_path + "/" + filename) |
| 57 | + |
| 58 | + except OSError: |
| 59 | + print("Error") |
| 60 | + finally: |
| 61 | + # When script is finished clear screen and display message |
| 62 | + print("Your files are organized now!") |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + try: |
| 67 | + dir_path = sys.argv[1] |
| 68 | + except Exception: |
| 69 | + print("Please enter directory path - python3 orgnize.py <path>") |
| 70 | + exit(0) |
| 71 | + organizeThis(dir_path) |
0 commit comments