cmd命令⾏修改⽂件内容(⽤来更改本地hosts)
主要⽤来修改本地hosts⽂件
1、将run.py⽂件放在C:\Users\Administrator⽬录下
2.打开cmd命令⾏。执⾏
python add 127.0.01
python del 127.0.01
内容
# -*- coding: cp936 -*-
# 防⽌命令⾏显⽰中⽂乱码
import sys
import os
path = "⽂件路径"
def add(ip , domain): # 在⽂件下增加内容
try:
with open(path, 'a+') as text:
for line adlines():
if ip + " "+ domain  in line:
print("已经存在")
break
text.write("\n"+ ip + " "+ domain)
except:
print("添加出错")
def delete(ip , domain):
with open(path) as text, open('2.txt', "w") as new_text:
try:
for line adlines():
new_line = place("\n"+ip + " "+ domain, "")
new_text.write(new_line)
except:
print("删除出错")
if __name__ == '__main__':
command = sys.argv[1].lower() # 参数⼀
ip = sys.argv[2] # 参数⼆
domain = sys.argv[3] # 参数三
提交更改是什么
if command == "add":
add(ip, domain)
elif command == "del":
delete(ip, domain)
else:
print("没有此命令")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。