Search
 
SCRIPT & CODE EXAMPLE
 

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!")
Comment

PREVIOUS NEXT
Code Example
Python :: how to increase width of line in graph of linear regression in matplotlib 
Python :: Closing small holes in the binary image with opencv 
Python :: python multiprocessing imap tqdm 
Python :: find mean of list python 
Python :: python join multiple strings ignore none and empty string 
Python :: sneon dr pepper 
Python :: theano_flags windows 
Python :: place a number randomly in a list python 
Python :: examples of function decorators in Python 
Python :: pyqgis 
Python :: example of input int questions in python with if statement 
Python :: multi line cooment in python 
Python :: when i was a young lad i was bitten by a turtle 
Python :: python calculate variance by hand 
Python :: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly 
Python :: print fps in while loop python 
Python :: does python have a end of line symbol 
Python :: explorer gives new window 
Python :: template strings in python 
Python :: getting player input python 
Python :: new listnode(0) meaning 
Python :: how to get only non-blank entry of list in python 
Python :: matplot lib mehrere bilder nebeneinander 
Python :: how to make turtle shape image smaller 
Python :: if query empty print python 
Python :: list of google colab deep learning tutorial 
Python :: get a liste from a txt file python 
Python :: python find duplicated zip files 
Python :: parquet folder single df dataframe 
Python :: python get all items from generator 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =