-
Notifications
You must be signed in to change notification settings - Fork 0
/
organiseFiles.py
60 lines (51 loc) · 1.79 KB
/
organiseFiles.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
# 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):
#print(type(sourcePath))
print(sourcePath)
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:]
#print(fileExtension,filename)
# 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)
#print(destinationName,destinationPath)
# 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(directory,extensionToDir):
# if len(sys.argv) != 2:
# print("Usage: <program> <source path directory>")
# return
#print(os.getcwd())
#sourcePath = sys.argv[1]
#extensionToDir = {}
#extensionToDir["pdf"] = "PDFs"
#extensionToDir["jpg"] = "Images"
#print(sourcePath)
print("")
OrganizeDirectory(directory, extensionToDir)
def calledFromOther(directory,typeFolder):
main(directory,typeFolder)
if __name__ == "__main__":
main()