- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace
More file actions
Latest commit
executable file
·24 lines (20 loc) · 586 Bytes
/
Copy pathreplace
File metadata and controls
executable file
·24 lines (20 loc) · 586 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
frompathlibimportPath
fromsysimportargv
iflen(argv) !=4:
print("Usage: ./replace [PATH] [OLDPATTERN] [NEWPATTERN]")
exit()
path=argv[1]
oldPattern=argv[2]
newPattern=argv[3]
ifPath.exists(Path(path)):
foriinPath.rglob(Path(path), oldPattern+'*'):
fullPath=i.parent.absolute()
rest=str(i).split(oldPattern, 1)[1]
newPath=i.parent.absolute() /Path(newPattern+rest)
i.rename(newPath)
data= (newPath.read_text()).replace(oldPattern, newPattern)
newPath.write_text(data)
else:
print("Given path does not exist")
exit()