zoukankan      html  css  js  c++  java
  • Python使用psutil模块,做你的电脑管家

    电脑管家

    也许大家都有这样的感觉,优化完美的电脑系统,你把电脑借给一个电脑小白使用上几天,等你拿回来的时候会发现,开机各种慢,乱七八糟的软件装了一大堆。那么我们如何使用Python来获取电脑的相关数据呢?不妨了解下psutil模块!

    psutil学习

    psutil是一个跨平台库(http://pythonhosted.org/psutil/) 能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统.

    模块安装

    使用pip install psutil

    查看磁盘分区

    import psutil
    
    disks = psutil.disk_partitions()
    for disk in disks:
        print(disk)
    
    >>> sdiskpart(device='C:\', mountpoint='C:\', fstype='NTFS', opts='rw,fixed')
    >>> sdiskpart(device='D:\', mountpoint='D:\', fstype='NTFS', opts='rw,fixed')
    >>> sdiskpart(device='E:\', mountpoint='E:\', fstype='NTFS', opts='rw,fixed')
    >>> sdiskpart(device='F:\', mountpoint='F:\', fstype='NTFS', opts='rw,fixed')

    查看磁盘使用率

    import psutil
    
    disks = psutil.disk_partitions()
    for disk in disks:
        print(disk.device, psutil.disk_usage(disk.device))
    >>> C: sdiskusage(total=64428584960, used=39714340864, free=24714244096, percent=61.6)
    >>> D: sdiskusage(total=107389222912, used=44705517568, free=62683705344, percent=41.6)
    >>> E: sdiskusage(total=322134831104, used=103709868032, free=218424963072, percent=32.2)
    >>> F: sdiskusage(total=506249498624, used=259100221440, free=247149277184, percent

    查看磁盘的IO

    import psutil
    
    io = psutil.disk_io_counters()
    print('磁盘IO:', io)
    print('数据类型:', type(io), '
    ')
    
    >>> 磁盘IO: sdiskio(read_count=169062, write_count=69826, read_bytes=7126855680, write_bytes=2237599744, read_time=741, write_time=163)
    >>> 数据类型: <class 'psutil._common.sdiskio'>

    获取CPU信息

    import psutil
    
    # cpu的完整信息
    print(psutil.cpu_times())
    # CPU逻辑个数
    print(psutil.cpu_count())
    # cpu使用率
    print(psutil.cpu_percent())
    
    >>> scputimes(user=1148.3389611, system=479.95267660000536, idle=43888.806536699994, interrupt=17.752913799999998, dpc=18.345717599999997)
    >>> 4
    >>> 3.5

    获取内存信息

    import psutil
    
    mem = psutil.virtual_memory()
    print(mem)
    print(mem.total/1024/1024)
    print(mem.total)
    print(mem.used)
    print(mem.free)
    
    >>> svmem(total=8478351360, available=4468076544, percent=47.3, used=4010274816, free=4468076544)
    >>> 8085.5859375
    >>> 8478351360
    >>> 4010274816
    >>> 4468076544

    获取开机时间

    import psutil
    from datetime import datetime
    
    print(psutil.boot_time())
    print(datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H: %M: %S"))
    
    >>> 1566915328.0
    >>> 2019-08-27 22: 15: 28

    查看系统进程信息

    import psutil
    
    for pid in psutil.pids():
        p = psutil.Process(pid)
        print(p.name())
        print(p.as_dict())
    
    >>> python.exe
    >>> {'exe': 'D:\Python37\python.exe', 'memory_full_info': None, 'ionice': ...
    >>> chrome.exe
    >>> {'exe': 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' ...
    >>> notepad++.exe
    >>> {'exe': 'F:\Software\Notepad++\notepad++.exe', 'memory_full_info': None, ...

    The End

    OK,今天的内容就到这里,如果觉得内容对你有所帮助,欢迎点赞。
    期待你关注我的公众号清风Python,如果觉得不错,希望能动动手指转发给你身边的朋友们。

    作者:清风Python

  • 相关阅读:
    Java学习开篇
    《我的姐姐》
    世上本无事,庸人自扰之
    这48小时
    补觉
    淡定
    es java api 设置index mapping 报错 mapping source must be pairs of fieldnames and properties definition.
    java mongodb groupby分组查询
    linux 常用命令
    mongodb too many users are authenticated
  • 原文地址:https://www.cnblogs.com/huaweicloud/p/11861420.html
Copyright © 2011-2022 走看看