简体   繁体   English

在python中打印USB的安装点时出现问题

[英]problem printing mount point of usb in python

I have a program written in python that uses dbus to detect inserted usb drives and prints the dir they are mounted on when they are detected. 我有一个用python编写的程序,该程序使用dbus来检测插入的USB驱动器,并在检测到它们时打印安装在其上的目录。 Here is the code: 这是代码:

import dbus
import gobject
import shutil
import os
import subprocess
import time

class DeviceAddedListener:
    def __init__(self):
    self.bus = dbus.SystemBus()
        self.hal_manager_obj = self.bus.get_object(
                                              "org.freedesktop.Hal", 
                                              "/org/freedesktop/Hal/Manager")
        self.hal_manager = dbus.Interface(self.hal_manager_obj,
                                          "org.freedesktop.Hal.Manager")
    self.hal_manager.connect_to_signal("DeviceAdded", self._filter)

    def _filter(self, udi):
        device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
        device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")

        if device.QueryCapability("volume"):
            return self.do_something(device)

    def do_something(self, volume):
        device_file = volume.GetProperty("block.device")
        label = volume.GetProperty("volume.label")
        fstype = volume.GetProperty("volume.fstype")
        mounted = volume.GetProperty("volume.is_mounted")
        mount_point = volume.GetProperty("volume.mount_point")
        try:
            size = volume.GetProperty("volume.size")
        except:
            size = 0
    p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["grep", device_file], stdin=p1.stdout, stdout=subprocess.PIPE)
    p3 = subprocess.Popen(["awk", "{ print $6 }"], stdin=p2.stdout, stdout=subprocess.PIPE)
    path = p3.communicate()[0]
    print path



if __name__ == '__main__':
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    loop = gobject.MainLoop()
    DeviceAddedListener()
    loop.run()

The problem is that when I print the path variable (the mount point of the usb), it prints an empty string. 问题是当我打印路径变量(USB的安装点)时,它会打印一个空字符串。 However, when I execute these same commands (Popen(), etc) in the python interactive interpreter, it prints the path just fine (/media/03CB-604C). 但是,当我在python交互式解释器中执行这些相同的命令(Popen()等)时,它会很好地打印路径(/ media / 03CB-604C)。 Why does this occur? 为什么会发生这种情况? Any edits / suggestions to my code would be much appreciated. 我的代码的任何编辑/建议将不胜感激。 Thanks in advance! 提前致谢!

In your original question it seems likely that you're being beaten by a race condition. 在您最初提出的问题中,您似乎有可能被种族条件殴打。

The device is inserted and your code is executed before the mounting process has completed. 在安装过程完成之前,已插入设备并执行了代码。

Try putting the Popen calls in a while loop (see below). 尝试将Popen调用置于while循环中(请参见下文)。

path = ""
count = 0
while count < 10 and path == "":
    p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["grep", device_file], stdin=p1.stdout, stdout=subprocess.PIPE)
    p3 = subprocess.Popen(["awk", "{ print $6 }"], stdin=p2.stdout, stdout=subprocess.PIPE)
    path = p3.communicate()[0]
    count += 1
    if path == "":
        time.sleep(1)
print path

This is a bit of a resource-hungry solution, but it should do what you want. 这有点耗费资源,但是它应该做您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM