替换host文件配置

来源:互联网 发布:淘宝企业店铺要钱吗 编辑:程序博客网 时间:2024/05/16 11:39
# -*- coding:utf8 -*-
import os#替换host文件配置def change_host():    print('请输入要切换的host配置:\n')    file_dict = {}    n = 1    #该方法对于每个目录返回一个三元组,(dirpath, dirnames, filenames)。    # 第一个是路径,第二个是路径下面的目录,第三个是路径下面的非目录(对于windows来说也就是文件)    for parent, dirname, filenames in os.walk(r'D:\host'):        for file in filenames:            print('%s. %s' %(n,file))            file_dict[n]=file            n = n + 1    # print(file_dict)    choose = input()    hostName = file_dict[int(choose)]    #不同的配置方案保存在不同的文件里    with open(r'D:\host\\' + hostName, 'r') as f:        host_config = f.read()    with open(r'C:\WINDOWS\System32\drivers\etc\hosts', 'w') as f:        f.write(host_config)change_host()
0 0