国产探花免费观看_亚洲丰满少妇自慰呻吟_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")

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆化县| 镶黄旗| 临澧县| 宿州市| 名山县| 瑞昌市| 新绛县| 策勒县| 葫芦岛市| 南充市| 宁武县| 当阳市| 雅江县| 广元市| 武胜县| 安塞县| 峨眉山市| 繁昌县| 苍南县| 罗平县| 永靖县| 云和县| 沭阳县| 顺昌县| 通海县| 蓝田县| 洛川县| 伊宁县| 大渡口区| 水富县| 牟定县| 五莲县| 勃利县| 天津市| 柘城县| 温宿县| 称多县| 西青区| 安塞县| 稻城县| 辽宁省|