Computer >> Máy Tính >  >> Lập trình >> Python

Trình tổ chức tệp rác trong Python?

Điều này có vẻ rất hữu ích đối với một lập trình viên python lười biếng, người giữ hầu hết các tệp và thư mục tại một vị trí và đôi khi nhầm lẫn tất cả các tệp ở đó là gì và chắc chắn anh ta quá lười biếng để làm điều đó theo cách thủ công. Vì vậy, dưới đây là một chương trình python để sắp xếp hoặc đơn giản hóa mọi thứ trong thư mục thích hợp chỉ trong một lần và xóa các thư mục trống.

Vì vậy, chúng tôi có một đường dẫn thư mục chứa rất nhiều tệp thuộc các loại khác nhau (như bên dưới) và chương trình của chúng tôi, chúng tôi tách từng loại tệp thành các thư mục tương ứng của chúng (như bên dưới).

Cấu trúc thư mục đầu vào

Trình tổ chức tệp rác trong Python?

Đầu ra mong muốn

Trình tổ chức tệp rác trong Python?

Đầu tiên, hãy tạo các thư mục khác nhau dựa trên các loại tệp, chúng tôi sẽ tách riêng thành các thư mục khác nhau:

DIRECTORIES = {
   "HTML": [".html5", ".html", ".htm", ".xhtml"],
   "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg",
   ".heif", ".psd"],
   "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng",
   ".qt", ".mpg", ".mpeg", ".3gp"],
   "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods",
   ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox",
   ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt",
   "pptx"],
   "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z",
   ".dmg", ".rar", ".xar", ".zip"],
   "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3",
   ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"],
   "PLAINTEXT": [".txt", ".in", ".out"],
   "PDF": [".pdf"],
   "PYTHON": [".py"],
   "XML": [".xml"],
   "EXE": [".exe"],
   "SHELL": [".sh"]
}

Thứ hai, tạo bản đồ loại tệp vào các thư mục tương ứng của chúng:

FILE_FORMATS = {file_format: directory
   for directory, file_formats in DIRECTORIES.items()
   for file_format in file_formats}
def organise_folder():
   for entry in os.scandir():
      if entry.is_dir():
         continue
      file_path = Path(entry)
      file_format = file_path.suffix.lower()
      if file_format in FILE_FORMATS:
         directory_path = Path(FILE_FORMATS[file_format])
         directory_path.mkdir(exist_ok=True)
         file_path.rename(directory_path.joinpath(file_path))

   try:
      os.mkdir("OTHER-FILES")
   except:
      pass

   for dir in os.scandir():
      try:
         if dir.is_dir():
            os.rmdir(dir)
         else:
            os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir)))
      except:
pass

Tập lệnh cuối cùng:

Vậy là xong, dưới đây là tập lệnh cuối cùng của chúng tôi để lọc các loại tệp vào các thư mục tương ứng.

#Python Lazy Junk Files Organizer

#Import important libraries
import os
from pathlib import Path

#
DIRECTORIES = {
   "HTML": [".html5", ".html", ".htm", ".xhtml"],
   "IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg",
   ".heif", ".psd"],
   "VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng",
   ".qt", ".mpg", ".mpeg", ".3gp"],
   "DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods",
   ".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox",
   ".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt",
   "pptx"],
   "ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z",
   ".dmg", ".rar", ".xar", ".zip"],
   "AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3",
   ".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"],
   "PLAINTEXT": [".txt", ".in", ".out"],
   "PDF": [".pdf"],
   "PYTHON": [".py"],
   "XML": [".xml"],
   "EXE": [".exe"],
   "SHELL": [".sh"]
}

FILE_FORMATS = {file_format: directory
   for directory, file_formats in DIRECTORIES.items()
   for file_format in file_formats}

def organise_folder():
   for entry in os.scandir():
      if entry.is_dir():
         continue
      file_path = Path(entry)
      file_format = file_path.suffix.lower()
      if file_format in FILE_FORMATS:
         directory_path = Path(FILE_FORMATS[file_format])
         directory_path.mkdir(exist_ok=True)
         file_path.rename(directory_path.joinpath(file_path))
   try:
      os.mkdir("OTHER-FILES")
   except:
      pass

   for dir in os.scandir():
      try:
         if dir.is_dir():
            os.rmdir(dir)
         else:
            os.rename(os.getcwd() + '/' + str(Path(dir)), os.getcwd() + '/OTHER-FILES/' + str(Path(dir)))
      except:
         pass
if __name__ == "__main__":
   organise_folder()

Sau khi chạy tập lệnh trên từ một đường dẫn thư mục cụ thể, chúng tôi sẽ nhận được đầu ra như sau,

Đầu ra

Trình tổ chức tệp rác trong Python?