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)文章
對(duì)變量賦值的理解--Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Pyton中讓兩個(gè)值互換的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-11-11PyTorch中model.zero_grad()和optimizer.zero_grad()用法
這篇文章主要介紹了PyTorch中model.zero_grad()和optimizer.zero_grad()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06Python學(xué)習(xí)筆記之lambda表達(dá)式用法詳解
這篇文章主要介紹了Python學(xué)習(xí)筆記之lambda表達(dá)式用法,結(jié)合實(shí)例形式詳細(xì)分析了lambda表達(dá)式的概念、功能、原理、組成及相關(guān)使用技巧,需要的朋友可以參考下2019-08-08Django使用unittest模塊進(jìn)行單元測(cè)試過程解析
這篇文章主要介紹了Django使用unittest模塊進(jìn)行單元測(cè)試過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python多進(jìn)程 主進(jìn)程和子進(jìn)程間共享和不共享全局變量實(shí)例
這篇文章主要介紹了python多進(jìn)程 主進(jìn)程和子進(jìn)程間共享和不共享全局變量實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-04-04Python 字符串處理特殊空格\xc2\xa0\t\n Non-breaking space
今天遇到一個(gè)問題,使用python的find函數(shù)尋找字符串中的第一個(gè)空格時(shí)沒有找到正確的位置,下面是解決方法,需要的朋友可以參考下2020-02-02python manage.py runserver流程解析
這篇文章主要介紹了python manage.py runserver流程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11