-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.py
More file actions
executable file
·26 lines (23 loc) · 862 Bytes
/
Copy pathreplace.py
File metadata and controls
executable file
·26 lines (23 loc) · 862 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
25
26
#!/usr/bin/env python
import os
import logging
def Replace(file_path, old_string, new_string):
with open(file_path, 'r') as f:
content = f.read()
with open(file_path, 'w') as f:
f.write(content.replace(old_string, new_string))
def FindAndReplace(root_dir, old_string, new_string, suffix=None):
root_dir = os.path.abspath(root_dir)
for root, _, files in os.walk(root_dir):
for f in files:
file_path = os.path.abspath(os.path.join(root, f))
if file_path == os.path.abspath(__file__):
continue
if suffix and not file_path.endswith(suffix):
continue
print file_path
try:
Replace(file_path, old_string, new_string)
except Exception as e:
logging.warning('Skip %s because of %s' % (file_path, repr(e)))
print 'Done converting %s -> %s' % (repr(old_string), repr(new_string))