-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_directory.py
executable file
·57 lines (50 loc) · 1.76 KB
/
clean_directory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
import os
from optparse import OptionParser
from ConfigParser import RawConfigParser
import shutil
parser = OptionParser()
config = RawConfigParser(allow_no_value=True)
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def pr_waring(text):
print bcolors.WARNING + '[WARNING] ' + bcolors.ENDC + text
def pr_info(text):
print bcolors.OKBLUE + '[INFO] ' + bcolors.ENDC + text
def main(directory):
extension_map = {}
print 'Sorting Directory: %s' % (directory)
config.read(os.environ['CLEAN_CONFIG_DIR'] + 'extension.cfg')
for section in config.sections():
for extension in config.options(section):
extension_map[extension] = section
tree = os.walk(directory)
tree = tree.next()
files = tree[2]
dirs = tree[1]
for f in files:
ext = f.split('.')[-1]
ext = ext.lower()
try:
file_type = extension_map[ext]
pr_info('Moving File %s to %s' % (f, file_type))
try:
os.stat('%s/%s' % (directory, file_type))
shutil.move('%s/%s' % (directory, f), '%s/%s/%s'%(directory, file_type, f))
except OSError:
os.mkdir('%s/%s' % (directory, file_type))
shutil.move('%s/%s' % (directory, f), '%s/%s/%s'%(directory, file_type, f))
except KeyError:
pr_waring('Unknown File extension %s' % (ext))
if __name__ == '__main__':
parser.add_option('-d', '--directory', dest = 'directory', help='Path to directory to sort', default='.')
(option, args) = parser.parse_args()
directory = option.directory
main(directory)