-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.py
63 lines (55 loc) · 2.25 KB
/
rename.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
import os
def rename(fldr,actn,strng):
dir=os.listdir(fldr)
fileindex=0
try:
for file in dir:
name=file[0:file.rindex('.')]
extension=file[file.rindex('.'):]
if actn=='r' and os.path.isfile(fldr+'/'+file): #rename
newname='{0}({1}){2}'.format(strng,fileindex,extension)
os.rename(fldr+'/'+file,fldr+'/'+newname)
fileindex+=1
if actn=='s': #strip
newname='{0}{1}'.format(name.replace(strng,''),extension)
os.rename(fldr+'/'+file,fldr+'/'+newname)
if actn=='a': #add
newname='{0}{1}{2}'.format(name,strng,extension)
os.rename(fldr+'/'+file,fldr+'/'+newname)
print('file ',file,' renamed to ',newname)
except Exception as e:
print(e,': some error occured, mostly because a file with new name already exists. program aborted.')
print("""rename all files in specified folder.
choose if you want to rename all the same (as in file(0).mp3, file(1).mp3, ...)
strip parts of filenames (as in file_1_security_copy.mp3, file_2_security_copy.mp3, ...)
add something before overwriting while copying into folder (as in file_1_a.mp3, file_2_a.mp3, file_3_a.mp3)
exit every time typing [exit]
all files in the given folder will be renamed regardless of file-type! folders remain untouched.
don't mess up your system.""")
folder=input('specify folder: ')
if folder=='exit':
print('bye...')
elif not os.path.isdir(folder):
print('folder ',folder,' not found, program aborted...')
else:
print('listing content of folder ',folder)
dir=os.listdir(folder)
for file in dir:
print(file, end='\t\t')
print('\n')
action=input('[r]ename, [s]trip part, [a]dd something? ')
if action=='r':
string=input('enter new filename to start renaming immediately: ')
elif action=='s':
string=input('enter to be stripped part to start renaming immediately: ')
elif action=='a':
string=input('enter part to be added to start renaming immediately: ')
else:
print('bye...')
exit()
if string=='exit':
print('bye...')
exit()
else:
rename(folder,action,string)
print('bye...')