Skip to content

Commit fa11e54

Browse files
committed
Added python script for organizing a directory
1 parent 2c1cfb6 commit fa11e54

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python3
2+
3+
import argparse
4+
import os
5+
6+
7+
def path():
8+
parse = argparse.ArgumentParser(
9+
add_help=True, description="Organize your files to different directories according to their type")
10+
parse.add_argument('directory_path', type=str, default='./',
11+
help="The absolute path to the directory")
12+
return parse.parse_args().directory_path
13+
14+
15+
documents = ['.log', '.txt', '.doc', '.docx', '.md', '.pdf', '.wps']
16+
picture = ['.png', '.jpg', 'jpeg', '.bmp']
17+
music = ['.mp3', '.wav']
18+
compressed = ['.zip', '.rar', '.tar', '.gz', '.bz2', '.xz']
19+
video = ['.3gp', '.mov', '.mp4', '.mkv', '.srt', '.avi']
20+
web = ['.html', .'.css', '.js']
21+
source = ['.py', '.c', '.cpp', '.java',]
22+
23+
24+
directories = [path() + '/Compressed', path() + '/Documents',
25+
path() + '/Pictures', path() + '/Music', path() + '/Video', path() + '/Web', path() + '/Source-codes',]
26+
27+
print("This will organize your files to different directories according to their type!!")
28+
print("Are you sure you want to continue? (y/n)")
29+
flag = input('>>>')
30+
if flag.lower() == 'y':
31+
try:
32+
for d in directories:
33+
os.mkdir(d)
34+
except FileExistsError:
35+
pass
36+
37+
for files in os.listdir(path()):
38+
dot = (files.rfind('.'))
39+
if dot is not 0 and dot is not -1:
40+
if files[dot:].lower() in music:
41+
os.rename(path() + '/' + files, path() + '/Music/' + files)
42+
if files[dot:].lower() in picture:
43+
os.rename(path() + '/' + files, path() + '/Pictures/' + files)
44+
if files[dot:].lower() in documents:
45+
os.rename(path() + '/' + files, path() + '/Documents/' + files)
46+
if files[dot:].lower() in compressed:
47+
os.rename(path() + '/' + files, path() +
48+
'/Compressed/' + files)
49+
if files[dot:].lower() in video:
50+
os.rename(path() + '/' + files, path() + '/Video/' + files)
51+
if files[dot:].lower() in web:
52+
os.rename(path() + '/' + files, path() + '/Web/' + files)
53+
if files[dot:].lower() in source:
54+
os.rename(path() + '/' + files, path() + '/Source-codes/' + files)
55+
56+
for d in directories:
57+
if os.listdir(d) is None:
58+
os.removedirs(d)
59+
else:
60+
print("Exiting")
61+
os.sys.exit(0)

Directory-organizer/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Directory-organizer
2+
Organises the files in the given directory according to their type(common extensions).
3+
Unrecognized extensions are kept in the parent directory itself while others are moved to respective new directories like 'Pictures' etc.
4+
5+
usage: Directory-oraganiser.py [-h] path_to_the_directory
6+
7+
8+

0 commit comments

Comments
 (0)