-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseparate.py~
58 lines (45 loc) · 1.83 KB
/
separate.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
# Ptyhon program to organize files of a directory
import os
import sys
import shutil
# This function organizes contents of sourcePath into multiple
# directories using the file types provided in extensionToDir
def OrganizeDirectory(sourcePath, extensionToDir):
if not os.path.exists(sourcePath):
print ("The source folder '" + sourcePath +
"' does not exist!!\n")
else:
for file in os.listdir(sourcePath):
file = os.path.join(sourcePath, file)
# Ignore if its a directory
if os.path.isdir(file):
continue
filename, fileExtension = os.path.splitext(file)
fileExtension = fileExtension[1:]
# If the file extension is present in the mapping
if fileExtension in extensionToDir:
# Store the corresponding directory name
destinationName = extensionToDir[fileExtension]
destinationPath = os.path.join(sourcePath, destinationName)
# If the directory does not exist
if not os.path.exists(destinationPath):
print ("Creating new directory for `" + fileExtension +
"` files, named - `" + destinationName + "'!!")
# Create a new directory
os.makedirs(destinationPath)
# Move the file
shutil.move(file, destinationPath)
def main():
if len(sys.argv) != 2:
print "Usage: <program> <source path directory>"
return
sourcePath = sys.argv[1]
extensionToDir = {}
extensionToDir["mp3"] = "Songs"
extensionToDir["jpg"] = "Images"
extensionToDir["c"] = "C Files"
extensionToDir["cpp"] = "C++ Files"
print("")
OrganizeDirectory(sourcePath, extensionToDir)
if __name__ == "__main__":
main()