修改hosts文件的小程序
修改hosts文件的小程序:
1 import os 2 import collections 3 4 def getAllDirQueue(path): 5 queue = collections.deque() 6 queue.append(path) 7 while len(queue) != 0: 8 dirPath = queue.popleft() 9 fileList = os.listdir(dirPath) # 找出所有的文件存入列表 10 for fileName in fileList: 11 fileAbsPath = os.path.join(dirPath, fileName) 12 if os.path.isdir(fileAbsPath): # 判断是否是目录,是目录就进队 13 queue.append(fileAbsPath) 14 else: 15 if fileName == 'hosts': 16 print('温馨提示:添加信息请输入insert,退出请输入exit!') 17 Tips = input('请输入你要执行什么操作:') 18 if Tips == 'insert': 19 while True: 20 info = input('请输入你要添加的信息:') # 比如:127.0.0.1 www.cnblogs.com 21 if info != 'exit': 22 with open(fileAbsPath, 'a') as f: 23 f.write(info + '\n') 24 else: 25 print('祝您工作顺利!') 26 break 27 elif Tips == 'exit': 28 print('祝您工作顺利!') 29 30 getAllDirQueue(r"C:\Windows\System32\drivers\etc")