python切换hosts文件代码示例


Posted in Python onDecember 31, 2013

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")
Python 相关文章推荐
简述Python中的面向对象编程的概念
Apr 27 Python
Python中list列表的一些进阶使用方法介绍
Aug 15 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 Python
深入浅析python继承问题
May 29 Python
Linux中安装Python的交互式解释器IPython的教程
Jun 13 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
Dec 12 Python
python开头的coding设置方法
Aug 08 Python
简单的Python调度器Schedule详解
Aug 30 Python
浅谈对pytroch中torch.autograd.backward的思考
Dec 27 Python
使用python的pyplot绘制函数实例
Feb 13 Python
基于PyTorch的permute和reshape/view的区别介绍
Jun 18 Python
什么是Python包的循环导入
Sep 08 Python
使用Python进行稳定可靠的文件操作详解
Dec 31 #Python
python连接mongodb操作数据示例(mongodb数据库配置类)
Dec 31 #Python
python连接mysql数据库示例(做增删改操作)
Dec 31 #Python
Python抓取Discuz!用户名脚本代码
Dec 30 #Python
python之模拟鼠标键盘动作具体实现
Dec 30 #Python
python多线程http下载实现示例
Dec 30 #Python
python正则匹配查询港澳通行证办理进度示例分享
Dec 27 #Python
You might like
如何使用PHP获取指定日期所在月的开始日期与结束日期
2013/08/01 PHP
PHP判断文章里是否有图片的简单方法
2014/07/26 PHP
php调用KyotoTycoon简单实例
2015/04/02 PHP
不要小看注释掉的JS 引起的安全问题
2008/12/27 Javascript
jQuery右侧选项卡焦点图片轮播特效代码分享
2015/09/05 Javascript
jQuery实现网页顶部固定导航效果代码
2015/12/24 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
基于jQuery实现淡入淡出效果轮播图
2020/07/31 Javascript
jQuery中的select操作详解
2016/11/29 Javascript
Vue和Bootstrap的整合思路详解
2017/06/30 Javascript
基于jQuery实现的单行公告活动轮播效果
2017/08/23 jQuery
JavaScript中使用参数个数实现重载功能
2017/09/01 Javascript
bootstrap表格内容过长时用省略号表示的解决方法
2017/11/21 Javascript
如何解决vue2.0下IE浏览器白屏问题
2018/09/13 Javascript
NodeJS加密解密及node-rsa加密解密用法详解
2018/10/12 NodeJs
Vue项目安装插件并保存
2019/01/28 Javascript
构建大型 Vue.js 项目的10条建议(小结)
2019/11/14 Javascript
在pycharm中开发vue的方法步骤
2020/03/04 Javascript
Python中使用logging模块代替print(logging简明指南)
2014/07/09 Python
在Python的Django框架中包装视图函数
2015/07/20 Python
Python numpy.zero() 初始化矩阵实例
2019/11/27 Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
2020/01/20 Python
Python异常继承关系和自定义异常实现代码实例
2020/02/20 Python
Python使用configparser库读取配置文件
2020/02/22 Python
python连接mysql有哪些方法
2020/06/24 Python
2019史上最全Database工程师题库
2015/12/06 面试题
博士学位自我鉴定范文
2013/12/26 职场文书
节约电力资源的建议书
2014/03/12 职场文书
在职党员进社区活动总结
2014/07/05 职场文书
2014党员学习兰辉先进事迹思想汇报
2014/09/17 职场文书
2014年保险业务员工作总结
2014/12/23 职场文书
离婚协议书的范本
2015/01/27 职场文书
管理失职检讨书范文
2015/05/05 职场文书
教你使用pyinstaller打包Python教程
2021/05/27 Python
关于html选择框创建占位符的问题
2021/06/09 HTML / CSS
Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
2022/03/21 Java/Android