0

Im trying to learn python, I haven't write this script but it work for my need, as I wanted to clean up my Download folder, the script move all the file to folder base on extension, which is nice, however the script got also moved to the folder

I'm trying to avoid that, my logic would be that

if file_format != 'cleardir.py':
    then skip this file

but I'm uncertain hence why I'd like your advise and explanation on your logic here is the code

#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"],
   'MAIL': ['.msg'],
   "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))
 #        elif:
 #            if file_format != 'cleardir.py':
 #                pass

   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()
Quentin D
  • 39
  • 1
  • 8

1 Answers1

0

You can use the python variable __file__ to refer to the script filename.

I think you can just change

   for entry in os.scandir():
       if entry.is_dir():
          continue

to

   for entry in os.scandir():
       if entry.is_dir() or entry.name == os.path.basename(__file__):
           continue

to skip the file.

nheise
  • 311
  • 1
  • 8
  • so I have to define the variable __file__ = name of my file to get it working? right now it move the file under Other-file directory – Quentin D Nov 19 '19 at 10:01
  • It's important to keep the underscores, so you'd use ```__file__``` and not ```file```. This will give the filepath for the current file - you don't need to define it yourself. See https://stackoverflow.com/a/9271481/11744541 for more info – nheise Nov 19 '19 at 10:19