写一个python脚本,我调用这个脚本,输入3个参数 就可以替换a.txt中的三个内容
时间: 2024-05-04 07:15:51 浏览: 12
当然,我可以帮您写这个脚本。以下是Python脚本代码:
```python
import sys
if len(sys.argv) != 4:
print("Usage: python replace.py <old_text_1> <new_text_1> <old_text_2> <new_text_2> <old_text_3> <new_text_3>")
sys.exit(1)
old_texts = [sys.argv[1], sys.argv[3], sys.argv[5]]
new_texts = [sys.argv[2], sys.argv[4], sys.argv[6]]
with open("a.txt", "r") as f:
file_contents = f.read()
for i in range(len(old_texts)):
file_contents = file_contents.replace(old_texts[i], new_texts[i])
with open("a.txt", "w") as f:
f.write(file_contents)
print("Done!")
```
您可以将这个脚本保存为 `replace.py` 文件,然后在命令行中运行:
```bash
python replace.py old_text_1 new_text_1 old_text_2 new_text_2 old_text_3 new_text_3
```
即可将 `a.txt` 文件中的三个文本替换为新的文本。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)