Skip to content

Instantly share code, notes, and snippets.

@FernandoCelmer
Last active September 23, 2021 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FernandoCelmer/8713c5bc8042cef3d42de79d0c705f25 to your computer and use it in GitHub Desktop.
Save FernandoCelmer/8713c5bc8042cef3d42de79d0c705f25 to your computer and use it in GitHub Desktop.
Script Python para mover determinados arquivos da pasta de Downloads para outras pastas conforme a sua extensão automaticamente.
# -*- coding: utf-8 -*-
"""
Script This File Folder
"""
import os
import time
import shutil
from dotenv import load_dotenv
class ThisFileFolder():
"""
Script This File Folder
"""
def __init__(self):
load_dotenv()
self.path = os.environ.get("PATH_THIS_FILE")
self.type_files = {
".sql": "SQL",
".zip": "ZIP",
".pdf": "PDF",
".xls": "XLS",
".jpeg": "IMG",
".png": "IMG",
".jpg": "IMG",
".psd": "EDIT",
".gif": "IMG",
".cdr": "EDIT",
".csv": "CSV",
".txt": "TXT",
".html": "HTML",
".docx": "DOC",
".odt": "DOC",
".xml": "XML",
".deb": "DEB",
".py": "CODE",
".xlsx": "XLS",
".json": "JSON",
"all": "ALL"
}
def run(self):
"""
Run script
"""
loop = True
while loop:
my_files = os.listdir(self.path)
for item in my_files:
name, ext = os.path.splitext(item)
try:
folder = self.type_files[ext]
except:
folder = 'ALL'
source = self.path + '{name_file}'.format(
name_file=item
)
destination = self.path + '{folder}/{name_file}'.format(
folder=folder,
name_file=item
)
num_file = 0
while os.path.isfile(destination):
num_file += 1
destination = self.path + '{folder}/{name_file}_{num_file}{ext_file}'.format(
folder=folder,
num_file=str(num_file),
name_file=item,
ext_file=ext
)
if ext != '':
try:
shutil.move(source,destination)
print("move: ", item)
except:
print("error: ", item)
# Wait 5 minutes
time.sleep(1)
if __name__ == "__main__":
script = ThisFileFolder()
script.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment