要求:好多文本文件进行批量替换,将”@@ “替换为“”
时间: 2023-05-15 21:07:37 浏览: 67
你可以使用 Python 编写一个脚本来实现这个功能。以下是示例代码:
```python
import os
# 遍历指定目录下的所有文件
def traverse_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
yield os.path.join(root, file)
# 批量替换文本文件中的指定字符串
def replace_string_in_files(directory, old_str, new_str):
for file_path in traverse_files(directory):
if file_path.endswith('.txt'):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
content = content.replace(old_str, new_str)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
# 替换指定目录下的所有文本文件中的"@@ "为""
replace_string_in_files('path/to/directory', '@@ ', '')
```
你只需要将 `path/to/directory` 替换为你要批量替换的文本文件所在的目录即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)