Skip to content

Commit 61addd2

Browse files
Merge pull request geekcomputers#1318 from sumanth-mandalapu/master
Added Downloaded Files Organizer
2 parents 7480b07 + 9f67997 commit 61addd2

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import psutil
2+
from obs import watcher
3+
browsers=["chrome.exe","firefox.exe","edge.exe","iexplore.exe"]
4+
5+
#ADD DOWNLOADS PATH HERE::: r is for raw string enter the path
6+
#Example: path_to_watch=r"C:\Users\Xyz\Downloads"
7+
#find downloads path .
8+
9+
10+
path_to_watch=r" "
11+
12+
13+
for browser in browsers:
14+
while browser in (process.name() for process in psutil.process_iter()):
15+
watcher(path_to_watch)
16+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import sys
3+
import shutil
4+
ext={"web":"css less scss wasm ",
5+
"audio":"aac aiff ape au flac gsm it m3u m4a mid mod mp3 mpa pls ra s3m sid wav wma xm ",
6+
"code":"c cc class clj cpp cs cxx el go h java lua m m4 php pl po py rb rs swift vb vcxproj xcodeproj xml diff patch html js ",
7+
"slide":"ppt odp ","sheet":"ods xls xlsx csv ics vcf ",
8+
"image":"3dm 3ds max bmp dds gif jpg jpeg png psd xcf tga thm tif tiff ai eps ps svg dwg dxf gpx kml kmz webp ",
9+
"archiv":"7z a apk ar bz2 cab cpio deb dmg egg gz iso jar lha mar pea rar rpm s7z shar tar tbz2 tgz tlz war whl xpi zip zipx xz pak ",
10+
"book":"mobi epub azw1 azw3 azw4 azw6 azw cbr cbz ",
11+
"text":"doc docx ebook log md msg odt org pages pdf rtf rst tex txt wpd wps ",
12+
"exec":"exe msi bin command sh bat crx ","font":"eot otf ttf woff woff2 ",
13+
"video":"3g2 3gp aaf asf avchd avi drc flv m2v m4p m4v mkv mng mov mp2 mp4 mpe mpeg mpg mpv mxf nsv ogg ogv ogm qt rm rmvb roq srt svi vob webm wmv yuv "}
14+
15+
for key,value in ext.items():
16+
value = value.split()
17+
ext[key]=value
18+
19+
20+
21+
def add_to_dir(ex,src_path,path):
22+
file_with_ex=os.path.basename(src_path)
23+
file_without_ex=file_with_ex[:file_with_ex.find(ex)-1]
24+
for cat,extensions in ext.items():
25+
if ex in extensions:
26+
os.chdir(path)
27+
dest_path=path+'\\'+cat
28+
if cat in os.listdir():
29+
try:
30+
shutil.move(src_path,dest_path)
31+
except shutil.Error:
32+
renamed_file = rename(file_without_ex,ex,dest_path)
33+
os.chdir(path)
34+
os.rename(file_with_ex,renamed_file)
35+
os.chdir(dest_path)
36+
shutil.move(path+'\\'+renamed_file,dest_path)
37+
else:
38+
os.mkdir(cat)
39+
40+
try :
41+
shutil.move(src_path,dest_path)
42+
except Exception as e:
43+
print(e)
44+
if os.path.exists(src_path):
45+
os.unlink(src_path)
46+
47+
48+
def rename(search,ex,dest_path):
49+
count=0
50+
os.chdir(dest_path)
51+
for filename in os.listdir():
52+
if filename.find(search,0,len(search)-1):
53+
count=count+1
54+
55+
return search+str(count)+'.'+ex

Downloaded Files Organizer/obs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def watcher(path):
2+
#python script to observe changes in a folder
3+
import sys
4+
import time
5+
import os
6+
from watchdog.observers import Observer
7+
from watchdog.events import FileSystemEventHandler
8+
from move_to_directory import add_to_dir
9+
10+
11+
class Handler(FileSystemEventHandler):
12+
def on_created(self,event):
13+
if event.event_type=="created":
14+
file_name = os.path.basename(event.src_path)
15+
ext = os.path.splitext(event.src_path)[1]
16+
time.sleep(2)
17+
add_to_dir(ext[1:],event.src_path,path)
18+
observer.stop()
19+
20+
21+
22+
observer = Observer()
23+
event_handler = Handler()
24+
observer.schedule(event_handler,path,recursive=True)
25+
observer.start()
26+
observer.join()
27+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Downloaded files organizer using python
2+
##### Operating System : Windows Only
3+
### Modules used
4+
1.Built-in Modules:
5+
sys,os,time
6+
2.External Modules:
7+
download the following modules using pip:
8+
psutil,watchdog
9+
### How to use
10+
1.After installing above modules,run browser_status.py.In browser_status.py makesure correct download path is given in this script.
11+
2.Make sure that all the three python scripts reside in same directory
12+
13+
### Working
14+
Whenever a file is downloaded,when this script is running ,it automatically categorize the file based on the file extension
15+
For example, if a python script is downloaded which has ".py" extension, it goes into "code" folder automatically.
16+
17+
The file extensions and categories are collected from
18+
https://github.com/dyne/file-extension-list

0 commit comments

Comments
 (0)