-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnamer.py
More file actions
executable file
·213 lines (195 loc) · 5.78 KB
/
namer.py
File metadata and controls
executable file
·213 lines (195 loc) · 5.78 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
namer.py
Select the option and let the program rename the files according to the option selected.
The idea of this program is to compile all of the common repetitive task of renaming files.
For example, if the option for remove underscore is 1 and the option to remove parentheses is 2,
you can use 2 command at once by typing "12" or 21.
"""
import os
import glob
option = []
def parentheses():
"""
remove all parentheses in file name
example:
file (1).txt => file 1.txt
"""
files = glob.glob("*(*).*")
for file in files:
new_name = file.replace("(", "")
new_name2 = new_name.replace(")", "")
print(new_name2)
os.rename(file, new_name2)
def underscore():
"""
remove all underscore in file name
example:
file_1.txt => file 1.txt
"""
files = glob.glob("*_*")
for file in files:
new_name = file.replace("_", " ")
os.rename(file, new_name)
def hd():
"""
remove all _HD in file name
example:
video_HD.mp4 => video.mp4
"""
files = glob.glob("*_HD*")
for file in files:
new_name = file.replace("_HD", "")
os.rename(file, new_name)
def replace():
"""
replace something in file name
example:
>>> Search: Asd
>>> Replace: Def
fileAsd.txt => fileDef.txt
Delete something in file name
example:
>>> Search: Asd
>>> Replace:
fileAsd.txt => file.txt
"""
search = input("Search: ")
replace = input("Replace: ")
files = glob.glob("*" + search + "*")
for file in files:
new_name = file.replace(search, replace)
os.rename(file, new_name)
def random():
"""
randomize file name
example:
>>> How many digit?: 5
file.txt => 78349.txt
"""
import random
while True:
try:
retry = 0
digit = int(input("How many digit?: "))
files = glob.glob("*.*")
for file in files:
# number creator
new_name_list = []
name_list = [] # name for all files
temp_name = []
ext = file.split(".")[1]
for i in range(digit):
randomNumber = str(random.randint(1, 9))
temp_name.append(randomNumber)
name_list.append("".join(temp_name)) # joining all the number
for name in name_list:
new_name_list.append(name + "." + ext) # adding file format into new_name_list
for name in new_name_list:
os.rename(file, name)
break
except FileExistsError:
print("FileExistsError: Input the digit again or increase the digit.")
def prefix():
"""
add prefix to file name
example:
>>> Prefix: Asd
file.txt => Asdfile.txt
"""
prefix = input("Prefix: ")
files = glob.glob("*.*")
for file in files:
os.rename(file, prefix+file)
print(file)
def suffix():
"""
add suffix to file name
example:
>>> Suffix: Asd
file.txt => fileAsd.txt
"""
suffix = input("Suffix: ")
files = glob.glob("*.*")
for file in files:
os.rename(file, file+suffix)
print(file)
# the problem with prefix() is that it will add prefix to ALL files. with number() it will add prefix to matching filename
def number(): # add zero prefix => if desired digit is n, search file with n-1 digit then add 1 zero prefix so desired digit achieved
"""
Add zero(s) to file name
example:
>>> Desired digit?: 3
01.jpeg => 001.jpeg
"""
digit = int(input("Desired digit?: "))
files = glob.glob("[0-9]" * (digit-1) + (".*")) # search file with n-1 digit
for file in files:
os.rename(file, "0"+file) # add 1 zero prefix
def text():
"""
Rename files according to a txt file.
example:
>>> File: file.txt
Separate search and replace with |
file1.txt|data.txt
file2.txt|log.txt
file1.txt => data.txt
file2.txt => log.txt
"""
text_file = input("File: ")
with open(text_file, "r") as f:
for line in f:
search = line.split("|")[0].replace("\n", "")
replace = line.split("|")[1].replace("\n", "")
os.rename(search, replace)
Change
# main program
def main():
# program user interface
print("""
1. Parentheses remover
2. Underscore remover
3. Tubemate"s _HD remover
4. Search and replace
5. File name randomizer
6. Prefix
7. Suffix
8. Number Namer
9. Renaming From A Text File
""")
# option parser
select = input("Option: ")
for item in select:
option.append(item) # pushing the option into the list
for options in option: # executing each option in the list
if options == "1": # calling the function name
parentheses()
print("Command " + options + " success!")
elif options == "2":
underscore()
print("Command " + options + " success!")
elif options == "3":
hd()
print("Command " + options + " success!")
elif options == "4":
replace()
print("Command " + options + " success!")
elif options == "5":
random()
print("Command " + options + " success!")
elif options == "6":
prefix()
print("Command " + options + " success!")
elif options == "7":
suffix()
print("Command " + options + " success!")
elif options == "8":
number()
print("Command " + options + " success!")
elif options == "9":
text()
print("Command " + options + " success!")
else:
print("Command " + options + " not found!")
if __name__ == "__main__":
main()