python小工具之读取host文件

# -*- coding: utf-8 -*-
# @Time : 2018/9/12 21:09
# @Author : cxa
# @File : readhostfile.py
# @Software: PyCharm
import platform as p
import os
# print(os.system())
p1 = p.system()
path = 'C:\Windows\System32\drivers\etc'
os.chdir(path)
file = 'hosts'

def read_hosts():
    with open(file, 'r') as fs:
        data = fs.readlines()
    return data


if __name__ == '__main__':
    if p1 == "Windows":
        datas = read_hosts()
        for data in datas:
            print(data)

  

 

因为直接读hosts文件没有权限所以使用了以下方法。

path = 'C:\Windows\System32\drivers\etc'
os.chdir(path)
file = 'hosts'
posted @ 2018-09-12 21:23  公众号python学习开发  阅读(1072)  评论(0编辑  收藏  举报