首页 > 解决方案 > 如果没有监视器插入 PI,psutil 将无法运行

问题描述

我有一个树莓派,它在重新启动时会运行 pyudev 和 psutil 脚本来查找可移动存储设备。如果插入了监视器,该脚本运行得非常好。但是一旦我拔下屏幕并重新启动 PI,它就不会加载 psutil。我什至找不到错误是什么,因为错误输出是:

>@>@>@>@>@>@>@>@>@>@>@

这是我的脚本:

def checkstorage():
    context = pyudev.Context()
    removable = [device for device in context.list_devices(subsystem='block', DEVTYPE='disk') if device.attributes.asstring('removable') == "1"]
    for device in removable:
        partitions = [device.device_node for device in context.list_devices(subsystem='block', DEVTYPE='partition', parent=device)]
        print("All removable partitions: {}".format(", ".join(partitions)))
        print("Mounted removable partitions:")
        for p in psutil.disk_partitions():
            if p.device in partitions:
                print(" {}: {}".format(p.device, p.mountpoint))
                if p.device == "/dev/sda1":
                    path = p.mountpoint
                else:
                    path = 0
                return path, True

它失败了:

for p in psutil.disk_partitions()

如果没有插HDMI屏

标签: pythonraspberry-pi4psutil

解决方案


问题是,一旦显示器被拔掉,PI 就像一个无头 PI。该实习生导致 psutil 失败,因为没有与 USB 关联的分区。我通过手动将 USB 安装到我选择的特定路径来解决此问题。


推荐阅读