python切换hosts文件代码示例_python

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")

时间: 2024-09-15 00:18:52

python切换hosts文件代码示例_python的相关文章

通过python切换hosts文件

做开发或测试时常需要切换hosts ,如果hosts比较多,那么频繁的打开hosts文件对地址加注释(#),再把去掉注释是个繁琐的事情. 当然,SwitchHosts 已经可以帮我们方便的解决了这个繁琐的事情. https://github.com/oldj/SwitchHosts 但笔者还是自己尝试用python写个小程序来实现切换.以需求为驱动来解决日常的问题是件非常有意思的事.   假如我们有一组hosts: 172.168.12.107 www.baidu.com 172.168.10.

Python实现生成简单的Makefile文件代码示例_python

在linux下写几个测试程序,还要一行行的输入g++命令进行编译,当经常改测试代码的时候,那一次次的敲(或者一次次的上线箭头选)也感觉不爽,不如make来的快.用Makefile的好处就不用多说了,这里我写了个脚本,其功能是自动搜索当前目录(不包括子目录)下的".c"文件生成Makefile文件. 代码在这里,功能有限(适用于单个文件是一个独立的测试代码的情况),需要的朋友可以稍作修改以满足需求. 复制代码 代码如下: #! /usr/bin/python '''  File     

使用Python读写及压缩和解压缩文件的示例_python

读写文件首先看一个例子: f = open('thefile.txt','w') #以写方式打开, try: f.write('wokao') finally: f.close() 文件的打开方式: f = open('文件','mode') 'r':只读(缺省.如果文件不存在,则抛出错误) 'w':只写(如果文件不存在,则自动创建文件),此时无法调用f.read()方法,且当调用f.write()时,将清空文件原有内容 'a':附加到文件末尾 'r+':读写 如果需要以二进制方式打开文件,需要

系统多种资源文件代码示例

示例 (一).功能  当一个系统有了一定规模,可能要销售到国外其它国家,这时候要实现多种资源文件.  本代码示例将介绍怎样实现: 一个系统同时具有简体,繁体,英文等不同资源文件.  实现原理:   将多资源文件存储在多个*.txt文件(例如CN.txt En.txt等)中,程序根据当前当前浏览器  语言设置 读取相应的资源文件(二).具体步骤如下  1. 创建一个资源文件     a.建立一个记事本文件: a.txt,并在里面写入:  _name=姓名       b.选"文件"-&g

python 远程统计文件代码分享

  享一个Python获取远程文件大小的函数代码,简单实用,是学习Python编程的基础实例. python 远程统计文件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #!/usr/bin/python #encoding=utf-8 import time import os import parami

Python读写Excel文件的实例_python

最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理.首先贴出网上找来的读写Excel的脚本.1.读取Excel(需要安装xlrd): #-*- coding: utf8 -*- import xlrd fname = "reflect.xls" bk = xlrd.open_workbook(fname) shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name(&

python实现绘制树枝简单示例_python

python是解释型语言,本文介绍了Python下利用turtle实现绘图功能的示例,本例所示为Python绘制一个树枝,具体实现代码如下:      python是解释型语言,本文介绍了Python下利用turtle实现绘图功能的示例,本例所示为Python绘制一个树枝,具体实现代码如下: import turtle def branch(length,level): if level<=0: return turtle.forward(length) turtle.left(45) bran

Python格式化css文件的方法_python

本文实例讲述了Python格式化css文件的方法.分享给大家供大家参考.具体实现方法如下: import string, sys import re, StringIO TAB=4 def format(ss): f = open (ss, "r") data = f.read() f.close() dlen = len(data) i = 0 buf = StringIO.StringIO() start = 0 while i < dlen: if data[i] == '

快速排序的算法思想及Python版快速排序的实现示例_python

快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). 1.分治法的基本思想 分治法的基本思想是:将原问题分解为若干个规模更小但结构与原问题相似的子问题.递归地解这些子问题,然后将这些子问题的解组合为原问题的解. 2.快速排序的基本思想 设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为: (1)分解: 在R[low..high]中任选一个记录作为基准(