Au­to­mate File Clas­si­fi­ca­tion with this Python Pro­gram

Un­clut­ter your Down­loads folder in Win­dows with this ex­tremely use­ful file clas­si­fier which is writ­ten in Python. Af­ter run­ning this script, you will be able to man­age your down­loads with ease.

OpenSource For You - - Contents - By: Mo­hit The au­thor is a cer­ti­fied eth­i­cal hacker and EC Coun­cil cer­ti­fied se­cu­rity an­a­lyst. He has a mas­ter’s de­gree in com­puter sci­ence from Tha­par Univer­sity. You can con­tact him at mo­hi­ and­hit-990a852a/

In­ter­net users down­load sev­eral files to the Down­loads folder of Win­dows OS. If you ex­am­ine the con­tents of your Down­loads folder (C:\Users\<user-name>\Down­loads) you might find var­i­ous files in di­verse for­mats like MP3, PDF, docs, srt, Zip, MP4, etc. Th­ese files pop­u­late your C drive, and then you start mov­ing the files from the Down­loads folder to other fold­ers, which can be a very tire­some and mo­not­o­nous task.

In this ar­ti­cle, I am go­ing to show you how to shift your files from the Down­loads folder to their re­spec­tive fold­ers au­to­mat­i­cally, by us­ing a sim­ple Python pro­gram.

The struc­ture of the pro­gram

The com­plete pro­gram con­tains a Python code and one txt file. The txt file sup­plies the nec­es­sary pa­ram­e­ters and rules for mov­ing the files. First, let’s write a text file called rules.txt, as shown be­low:

C:\Users\<user-name>\Down­loads R mp3:D:\mu­sic pdf:D:\al­l_pdf exe:D:\soft­ware doc:D:\al­l_­docs docx:D:\al­l_­docs srt:D:\al­l_s­rts mp4:D:\al­l_mp4

The first line in­di­cates the com­plete path of the Down­loads folder; so it is user-spe­cific. The se­cond line states the mode. I used ‘R’ for re­cur­sive mode and ‘S’ for sim­ple mode. In re­cur­sive mode, the in­ter­preter checks fold­ers in­side Down­loads. In sim­ple mode, the in­ter­preter checks the files only in the Down­loads folder.

The re­main­ing lines show the file ex­ten­sions with paths. This means that MP3 files must be moved to D:\mu­sic folder and so on.

Let us look at the pro­gram, and try and un­der­stand it:

# pro­gram cre­ated by mo­hit # of­fi­cial web­site L4wis­ # email-id mo­hi­

Im­port the manda­tory files:

im­port os im­port shutil

The fol­low­ing lines in­di­cate the path of the rules.txt file. In your Win­dows folder, this should be C:\Users\<user­name>\.


os.path.ex­panduser(‘~’) file_­name = file_­path+”//” +”rules.txt”

Get the path and mode from rules.txt:


= open(file_­name,’r’)­line() mode =­line() mode = mode.strip(“\n”).lower() path1 = path.strip(“\n”)

The fol­low­ing func­tion re­turns a dic­tionary that con­tains the rules. The dic­tionary’s keys be­have as ex­ten­sions of the files, and val­ues act as moved paths, re­spec­tively.

def rules(): dict1 = {} for each in file_t: each = each.strip(“\n”) if each.split(“:”,1)[0]:

file_ext,dest_­path = each. split(“:”,1) file_ext = file_ext.strip() dest_­path = dest_­path. strip() dict1[file_ext]=dest_­path re­turn dict1

The fol­low­ing func­tion takes a list of files, and moves the files to their re­spec­tive fold­ers:

def file_­move(files_list): for file in files_list : if “.” in file: ext = file.rsplit(“.”,1)[1] ext= ext.strip() if ext in dict1: dst = dict1[ext] try: print file shutil.move(file, dst)

ex­cept Ex­cep­tion as e : print e

The fol­low­ing func­tion is used when a sim­ple mode is se­lected:

def sin­gle_dir(path1): os.chdir(path1) files = os.list­dir(“.”) file_­move(files)

The fol­low­ing func­tion is used when a re­cur­sive mode is se­lected:

def rec_dirs(path1):

for root, dirs, files in os.walk(path1, top­down=True, on­error=None, fol­lowlinks=False):

#print files os.chdir(root) file_­move(files) print “files are moved” dict1 = rules()

if mode ==’r’:

rec_dirs(path1) else:


Run­ning the pro­gram

So, the pro­gram is ready.

Now let us con­vert the Python pro­gram into an .exe file.

In order to do this, use the in­staller shown in Fig­ure 1. Af­ter con­ver­sion, it will make a direc­tory called class1\dist, as shown in Fig­ure 2. Get the class1.exe files from the direc­tory class1\dist and put them in the Win­dows folder. Be­fore do­ing that, you can re­name the exe file. I am call­ing it cfr.exe. In this way, cfr.exe is added to the sys­tem path. cfr.exe works like a

DOS com­mand.

In order to run the pro­gram, use Win­dows’ run fa­cil­ity, as shown in Fig­ure 3.

Af­ter a suc­cess­ful run, check the fold­ers as spec­i­fied in the rules.txt. If the fold­ers do not ex­ist, then the pro­gram au­to­mat­i­cally cre­ates the fold­ers.

Fig­ure 3: Run the pro­gram

Fig­ure 4: Fold­ers are cre­ated and files are moved

Fig­ure 1: Con­vert­ing the Python pro­gram into an .exe file

Fig­ure 2: exe. files

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.