Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python automation to sort files

import os
import shutil

SOURCE = r"replacewithsourcedirectory" # source directory
DESTINATION = r"replacewithdestinationdirectory" # destination directory

os.chdir(SOURCE)

for file in os.listdir():
    # for each file in the source directory, move it to the destination directory

    if file.endswith(".mp4") or file.endswith(".avi") or file.endswith(".mkv") or file.endswith(".mov") or file.endswith(".mpg") or file.endswith(".mpeg"):
        if not os.path.exists(DESTINATION+"Videos"):
            os.makedirs(DESTINATION+"Videos")
        shutil.move(file, DESTINATION+"Videos")
    elif file.endswith(".jpg") or file.endswith(".png") or file.endswith(".gif") or file.endswith(".bmp") or file.endswith(".jpeg") or file.endswith(".JPG"):
        if not os.path.exists(DESTINATION+"Pictures"):
            os.makedirs(DESTINATION+"Pictures")
        shutil.move(file, DESTINATION+"Pictures")
    elif file.endswith(".pdf"):
        if not os.path.exists(DESTINATION+"PDFs"):
            os.makedirs(DESTINATION+"PDFs")
        shutil.move(file, DESTINATION+"PDFs")
    elif file.endswith(".doc") or file.endswith(".docx") or file.endswith(".txt") or file.endswith(".rtf"):
        if not os.path.exists(DESTINATION+"Docs"):
            os.makedirs(DESTINATION+"Docs")
        shutil.move(file, DESTINATION+"Docs")
    elif file.endswith(".zip") or file.endswith(".rar") or file.endswith(".7z"):
        if not os.path.exists(DESTINATION+"Archives"):
            os.makedirs(DESTINATION+"Archives")
        shutil.move(file, DESTINATION+"Archives")
    elif file.endswith(".mp3") or file.endswith(".wav") or file.endswith(".flac") or file.endswith(".aac") or file.endswith(".ogg"):
        if not os.path.exists(DESTINATION+"Music"):
            os.makedirs(DESTINATION+"Music")
        shutil.move(file, DESTINATION+"Music")
    elif file.endswith(".exe") or file.endswith(".msi") or file.endswith(".apk") or file.endswith(".deb") or file.endswith(".rpm"):
        if not os.path.exists(DESTINATION+"Programs"):
            os.makedirs(DESTINATION+"Programs")
        shutil.move(file, DESTINATION+"Programs")
    elif file.endswith(".iso") or file.endswith(".img") or file.endswith(".bin") or file.endswith(".iso") or file.endswith(".dsk"):
        if not os.path.exists(DESTINATION+'Disks'):
            os.makedirs(DESTINATION+'Disks')
        shutil.move(file, DESTINATION+'Disks')
    elif file.endswith(".torrent"):
        if not os.path.exists(DESTINATION+'Torrents'):
            os.makedirs(DESTINATION+'Torrents')
        shutil.move(file, DESTINATION+'Torrents')
    elif file.endswith("jdk") or file.endswith("jre") or file.endswith("jdk") or file.endswith("jre"):
        if not os.path.exists(DESTINATION+'JREs'):
            os.makedirs(DESTINATION+'JREs')
        shutil.move(file, DESTINATION+'JREs')
    elif file.endswith("xlsx") or file.endswith("xls") or file.endswith("csv") or file.endswith("xlsm") or file.endswith("xlsb"):
        if not os.path.exists(DESTINATION+'Spreadsheets'):
            os.makedirs(DESTINATION+'Spreadsheets')
        shutil.move(file, DESTINATION+'Spreadsheets')
    elif file.endswith("pptx") or file.endswith("ppt") or file.endswith("pptm") or file.endswith("ppsm") or file.endswith("ppsx"):
        if not os.path.exists(DESTINATION+'Presentations'):
            os.makedirs(DESTINATION+'Presentations')
        shutil.move(file, DESTINATION+'Presentations')
    elif file.endswith("epub") or file.endswith("mobi") or file.endswith("azw") or file.endswith("azw3") or file.endswith("azw4") or file.endswith("azw5") or file.endswith("azw6") or file.endswith("azw7") or file.endswith("azw8") or file.endswith("azw9") or file.endswith("azw10"):
        if not os.path.exists(DESTINATION+'Books'):
            os.makedirs(DESTINATION+'Books')
        shutil.move(file, DESTINATION+'Books')
    else:
        if not os.path.exists(DESTINATION+'Others'):
            os.makedirs(DESTINATION+'Others')
        shutil.move(file, DESTINATION+'Others')
print("Done!")
 
PREVIOUS NEXT
Tagged: #python #automation #sort #files
ADD COMMENT
Topic
Name
6+4 =