-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_rename.py
66 lines (56 loc) · 1.79 KB
/
dir_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
64
65
66
'''
Allows the user to rename all the files in a directory sequentially.
Adapted from a geeksforgeeks article.
'''
import os
def main():
#Get current working directory (CWD)
folder = ''
while folder != '!exit':
path = os.getcwd()
print('\nInput a directory to rename the files in it. The directory must be a subdirectory of the CWD.')
print('CWD = ' + path)
print('Type !change to change the current working directory.')
print('Type !exit to exit.')
folder = input('>')
if folder == '!exit':
break
elif folder == '':
print('\tError: Please input a directory name.')
elif folder == '!change':
newDir = input('Enter new CWD\n>')
try:
os.chdir(newDir)
except FileNotFoundError:
print('\tError: Directory not found.')
else:
print('\nInput a new base name for each file in this directory.\nFor example, \'foo\' will yield foo1.jpg, foo2.png, foo3.exe, etc.')
name = input('>')
if name == '':
print('\tError: Please input a file name.')
elif name == '!exit':
break
else:
#Concatenate the path and the folder
path = os.path.join(path, folder)
try:
#For each file in this directory...
for count, filename in enumerate(os.listdir(path)):
#Get the extension
splt = filename.split('.')
if len(splt) == 1:
ext = ''
else:
ext = splt[1]
#Concatenate source file path
src = os.path.join(path, filename)
#Assemble destination filename and concatenate its path
dst = name + str(count) + '.' + ext
dst = os.path.join(path, dst)
#print(src)
#print(dst)
#Rename
os.rename(src, dst)
except FileNotFoundError:
print('\tError: Directory does not exist.\n')
main()