-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSort.py
More file actions
127 lines (92 loc) · 3.01 KB
/
fileSort.py
File metadata and controls
127 lines (92 loc) · 3.01 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import os.path
import shutil
#
# Classifies the files
#
def classify_files(path, L) :
'''' Checks each file for key words to classify the file
'''
d = {}
for key in L:
d[key] = []
AllFiles = list(os.walk(path))
# print(AllFiles)
for item in AllFiles:
foldername, LoDirs, LoFiles = item # cool unpacking!
for filename in LoFiles:
if filename[-3:] == "txt":
fullfilename = foldername + "/" + filename
# print(fullfilename)
f = open(fullfilename, "r", encoding="latin1")
contents = f.read()
for grade in d:
if grade in contents:
d[grade].append(fullfilename)
f.close()
return d
#
# Makes new directories to store the different categories
#
def make_directories(dictionary) :
''' Makes new directories to store the different categories
'''
currentdir = os.getcwd()
# print(currentdir)
LoNewDir = []
joinedDir = []
for key in dictionary:
LoNewDir.append(key)
for newdir in LoNewDir:
if not os.path.exists(newdir):
joinedDir.append(os.path.join(currentdir, newdir))
print(f"joinedDir is {joinedDir}")
#
# Copies the recipes into their corresponding folder
#
def copy_files(dictionary) :
''' Moves copies of each recipe into its correct folder
'''
for category in dictionary :
print(f"category: {category} \n")
for file in dictionary[category]:
print(f"file: {file} \n")
try:
slicePlace = file.index("/")
filename = file[slicePlace+1:]
filedirec = file[:slicePlace]
print(f"filename: {filename} \n")
print(f"filedirec: {filedirec} \n")
newfilepath = os.path.join(currentdir, category, filename)
print(f"copying {file}")
print(f"to {newfilepath} \n")
if not os.path.exists(newfilepath):
shutil.copy(file,newfilepath)
except FileNotFoundError as e:
print(f"*** Copying did not work *** ")
print(f"Python's error message: {e}")
def addtoFile(file, name, descript, age, subject, country):
f = open(file, 'w')
f.write(f"Name: {name}")
f.write('\n')
f.write(f"Description: {descript}")
f.write('\n')
f.write(f"Age: {age}")
f.write('\n')
f.write(f"Subject: {subject}")
f.write('\n')
f.write(f"Country: {country}")
f.write('\n')
f.close()
if True:
# sign on
print(f"[[ Start! ]]\n")
currentdir = os.getcwd()
# countries = ["United States", "Canada"]
# d = classify_files(currentdir, countries)
# print(d)
# print()
# make_directories(d)
# print()
# copy_files(d)
# addtoFile(currentdir+'/Kindergarten.txt', "hihihi", "hihih", "hihih", "hi", "hihihihi")