-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreplace_string.py
More file actions
executable file
·62 lines (52 loc) · 2.19 KB
/
replace_string.py
File metadata and controls
executable file
·62 lines (52 loc) · 2.19 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
58
59
60
61
62
#!/usr/bin/env python3
import argparse
import re
from pathlib import Path
def process_file(file_path: Path, pattern: re.Pattern, replacement: str):
temp_file = file_path.with_suffix(file_path.suffix + ".tmp")
try:
modified = False
with open(file_path, 'r', encoding='utf-8') as fin, \
open(temp_file, 'w', encoding='utf-8') as fout:
for line in fin:
new_line = pattern.sub(replacement, line)
if new_line != line:
modified = True
fout.write(new_line)
if modified:
temp_file.replace(file_path)
print(f"[\x1b[32m已修改\x1b[0m] {file_path}")
else:
temp_file.unlink()
print(f"[無變動] {file_path}")
except (UnicodeDecodeError, PermissionError) as e:
print(f"[跳過] {file_path} (原因: {e})")
if temp_file.exists():
temp_file.unlink()
def main():
parser = argparse.ArgumentParser(description="正則表達式批量替換工具")
parser.add_argument("path", help="目標路徑")
parser.add_argument("pattern", help="正則表達式")
parser.add_argument("replacement", help="替換字串")
parser.add_argument("filter", nargs="?", default="*", help="檔案過濾 (預設: *)")
parser.add_argument("-r", "-R", "--recursive", action="store_true", help="遞迴掃描子資料夾")
args = parser.parse_args()
base_path = Path(args.path)
try:
pattern = re.compile(args.pattern)
except re.error as e:
print(f"正則表達式語法錯誤: {e}")
return
if base_path.is_file():
process_file(base_path, pattern, args.replacement)
elif base_path.is_dir():
# 根據是否遞迴選擇 glob 或 rglob
files = base_path.rglob(args.filter) if args.recursive else base_path.glob(args.filter)
print(f"模式: {'遞迴' if args.recursive else '僅當前目錄'}")
for file_path in files:
if file_path.is_file():
process_file(file_path, pattern, args.replacement)
else:
print(f"錯誤: 路徑不存在 '{base_path}'")
if __name__ == '__main__':
main()