python切換hosts文件代碼示例
win7以上需要使用管理員權(quán)限操作。
# -*- 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")
相關(guān)文章
對變量賦值的理解--Pyton中讓兩個值互換的實現(xiàn)方法
下面小編就為大家分享一篇Pyton中讓兩個值互換的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11PyTorch中model.zero_grad()和optimizer.zero_grad()用法
這篇文章主要介紹了PyTorch中model.zero_grad()和optimizer.zero_grad()用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python多進程 主進程和子進程間共享和不共享全局變量實例
這篇文章主要介紹了python多進程 主進程和子進程間共享和不共享全局變量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python 字符串處理特殊空格\xc2\xa0\t\n Non-breaking space
今天遇到一個問題,使用python的find函數(shù)尋找字符串中的第一個空格時沒有找到正確的位置,下面是解決方法,需要的朋友可以參考下2020-02-02python manage.py runserver流程解析
這篇文章主要介紹了python manage.py runserver流程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11