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:WindowsSystem32driversetc'
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:WindowsSystem32driversetc'
os.chdir(path)
file = 'hosts'
原文地址:https://www.cnblogs.com/c-x-a/p/9637466.html