-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
57 lines (35 loc) · 1.33 KB
/
Copy pathrun.py
File metadata and controls
57 lines (35 loc) · 1.33 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
import os
def main():
renameFilesToParentDir(r'C:\path\to\directory', '.txt')
def renameFilesToParentDir(dir, ext='all'):
"""
Renames all files inside the given directory and it's subdirectory to the name of the respective parent directory.
Arguments:
dir = directory to loop through
ext (optional) = specify if only specific extensions shall be considered (e.g. '.txt')
"""
for subdir, dirs, files in os.walk(dir):
count = 1
for file in files:
# Filepath
path = os.path.join(subdir, file)
print("OLD: " + path)
# Name of parent folder
parentFolder = os.path.basename(
os.path.normpath(os.path.dirname(path)))
# File extension
fileExt = os.path.splitext(path)[1]
considerFile = True
if ext != 'all' and not path.lower().endswith((ext)):
considerFile = False
if considerFile:
newFileName = parentFolder + " " + str(count) + fileExt
newPath = os.path.join(subdir, newFileName)
os.rename(path, newPath)
print("NEW: " + newPath)
count = count + 1
else:
print("Not renamed.")
print("---")
if __name__ == "__main__":
main()