国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python切換hosts文件代碼示例

2020-02-23 05:04:27
字體:
來源:轉載
供稿:網友

win7以上需要使用管理員權限操作。
代碼如下:
# -*- coding: utf-8 -*-
import os
import glob
import shutil

def format_file_list(files):
        all_files_str = ""
        for i in range(len(files)):
                all_files_str +=  str(i)+":"+files[i]+"/n"
        return all_files_str

hosts_path = "C://Windows//System32//drivers//etc"
files =  os.listdir(hosts_path)
os.chdir(hosts_path)

if os.getcwd() != hosts_path:
        print("Switch Dir to System32 Error,check permission!/npwd:"+os.getcwd())
        exit()

hosts_files = glob.glob("host*")
choosed_file_idx = int(input("Choose Hosts File Index:/n"+format_file_list(hosts_files)))
files_num = len(hosts_files)

if (choosed_file_idx < 0 or choosed_file_idx >= files_num) :
        print("Please choose a file in the lists!")
        exit()

print("Choosed idx:{0},file:{1}.".format(choosed_file_idx,hosts_files[choosed_file_idx]))
shutil.copy("hosts","hosts.bak")
shutil.copy(hosts_files[choosed_file_idx],"hosts")
print("Copy ok,then flush dns...")
os.system("ipconfig /flushdns")

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永昌县| 霍林郭勒市| 比如县| 阿拉善盟| 浦江县| 宁蒗| 拉孜县| 重庆市| 阿合奇县| 印江| 太白县| 阿合奇县| 积石山| 大化| 明水县| 法库县| 河北区| 阿拉善右旗| 德清县| 衡阳市| 晋江市| 镇赉县| 厦门市| 鹰潭市| 乐安县| 临潭县| 安徽省| 嘉黎县| 柏乡县| 岳西县| 中牟县| 宁强县| 绥江县| 琼结县| 南昌县| 时尚| 杭州市| 彭山县| 闻喜县| 新乡县| 龙川县|