用python实现带参数的脚本(修改hosts文件)

数据分析侠 2018-04-19

from os import path
 from sys import argv
 from getopt import getopt
 
 path=path.join("C:\Windows\System32\drivers\etc\hosts") #打开hosts文件
 
 opt,args=getopt(argv[1:])  #在文件后面跟上参数
 
 content="\n%s %s"%(args[0],args[1])   #拼接内容
 
 with open(path,'a') as file:
     file.write(content)           #往hosts文件里面添加内容

相关推荐