-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktop_organizer.py
97 lines (83 loc) · 3.56 KB
/
desktop_organizer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import time
import platform
import argparse
import logging
def main():
# Determines the Desktop Directory on Unix Base System and Windows
if is_windows():
path_to_watch = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
else:
path_to_watch = os.path.join(
os.path.join(os.path.expanduser('~')), 'Desktop')
before = dict([(f, None) for f in os.listdir(path_to_watch)])
args = parse_args()
video_path = os.path.join(path_to_watch, 'Videos')
picture_path = os.path.join(path_to_watch, 'Pictures')
misc_path = os.path.join(path_to_watch, 'Misc')
# Assign my Parse Arguments
if args.path:
path_to_watch = args.path
if args.pic:
picture_path = args.pic
if args.video:
video_path = args.video
if args.misc:
misc_path = args.misc
# The loop for event notification
while 1:
time.sleep(1)
after = dict([(f, None) for f in os.listdir(path_to_watch)])
added = [f for f in after if not f in before]
if added:
for files in added:
src_path = os.path.join(path_to_watch, files)
if not os.path.isdir(src_path):
if file_type(files) == 'VIDEO':
moved_to_path(src_path, os.path.join(video_path, files))
elif file_type(files) == 'PICTURE':
moved_to_path(src_path, os.path.join(picture_path, files))
elif file_type(files) == 'MISC':
moved_to_path(src_path, os.path.join(misc_path, files))
# Function for moving the file to desired path
def moved_to_path(source_file, destination_file):
# Logs for files
logging.basicConfig(filename='file_organizer.log', level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
dest_path = os.path.abspath(destination_file)
# Creating a Folder if the Destination File Path does not exist
if not os.path.exists(dest_path):
os.mkdir(dest_path)
print(f"the Path {dest_path} CREATED")
logging.info(f'{dest_path} folder has been CREATED!!!')
os.rename(source_file, destination_file)
logging.info('The directory {} is moved to {}'.format(source_file, destination_file))
# Function that will identify what is the file type
def file_type(filename):
# Common Video file types
video_type = ['mp4', 'avi', 'mkv', 'flv', 'webm', 'mov', 'swf', 'ogg', 'm4p', 'm4v']
# Common Picture type files
picture_type = ['jpg', 'jpeg', 'gif', 'png', 'heic']
type_of_file = filename.split('.').pop().lower()
if type_of_file in video_type:
return 'VIDEO'
elif type_of_file in picture_type:
return 'PICTURE'
else:
return 'MISC'
# Arguments
def parse_args():
parser = argparse.ArgumentParser(
description='This is an Automated Script for Moving Files to Specific Folder. NOTE that the directory must be created when you run the script')
parser.add_argument('-p', '--path', help='The Folder to Track')
parser.add_argument('-v', '--video', metavar='Path/to/Video/Directory', help='Video Files Directory')
parser.add_argument('-P', '--pic', metavar='Path/to/Picture/Directory', help='Picture files Directory')
parser.add_argument('-m', '--misc', help='Other Files', metavar='Path/to/Misc/Directory')
args = parser.parse_args()
return args
# Identify if the platform is on windows
def is_windows():
return platform.system() == 'Windows'
if __name__ == '__main__':
main()