-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeSubstringInFolderThree.py
More file actions
executable file
·57 lines (44 loc) · 1.57 KB
/
changeSubstringInFolderThree.py
File metadata and controls
executable file
·57 lines (44 loc) · 1.57 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
#!/usr/bin/env python
import os
import argparse
def changeSubstringInFolderThree(changeFrom="", changeTo=""):
"""
Walks through the folder three and changes substrings in
foldernames.
Parameters
----------
changeFrom : str
Old substring
changeTo : str
New substring
"""
# Appendable number of hits
hits = []
# Traverse root directory, and list directories as dirs and files as files
for root, _, _ in os.walk("."):
# Skip current folder
if root == ".":
continue
# Get last part of folder three
endOfThree = os.path.basename(root)
if changeFrom in endOfThree:
# Appent to hits
hits.append(root)
# Sort hits after decreasing occurence of folder separation
pathSep = os.path.sep
hits.sort(key=lambda x: x.count(pathSep), reverse = True)
for fromPath in hits:
toPath = fromPath.replace(changeFrom, changeTo)
print("Renaming {} -> {}".format(fromPath, toPath))
os.rename(fromPath, toPath)
if __name__ == "__main__":
# Arg parsing
parser = argparse.ArgumentParser(\
description=(("Walks through the folder three and changes"
"substrings in foldernames.")))
parser.add_argument("-f", "--changeFrom", default="", type=str,\
help="Old substring")
parser.add_argument("-t", "--changeTo", default="", type=str,\
help="New substring")
args = parser.parse_args()
changeSubstringInFolderThree(args.changeFrom, args.changeTo)