个人机器上用于hosts替换的python小脚本

来源:互联网 发布:淘宝店铺的货源怎么找 编辑:程序博客网 时间:2024/05/22 03:13

由于学校教育网时常出现问题导致ipv6不能访问,对于我这种靠v6访问Google的人来说,需要来回切换hosts文件。下面脚本用来执行hosts的替换。
p.s:学python之后的第一个小程序

import os.pathimport shutildef copyIpv6Hosts(sourceDir,targetDir):    for files in os.listdir(sourceDir):        sourcefile = os.path.join(sourceDir,files)        targetfile = os.path.join(targetDir,files)        if os.path.isfile(targetfile) and files=="hosts":            os.remove(targetfile)        if os.path.isfile(sourcefile) and files=="hosts":            shutil.copy(sourcefile,targetfile)def main():    sourceDir = "g:/ipv6hosts"    targetDir = "c:/Windows/System32/drivers/etc"    copyIpv6Hosts(sourceDir,targetDir)if __name__ == '__main__':    main()

上面用于替换成为v6,类似的,改变sourceDir的值就可以再替换为v4,当然脚本的运行需要在管理员的权限下。

1 0
原创粉丝点击