Hello,
I am writing a program in python that detects inserted usb drives using dbus in linux and copies all of the files from it over to a dir on the computer. Here's my code:

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

def move_files(pth):
	p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
	p2 = subprocess.Popen(["grep", pth], stdin=p1.stdout, stdout=subprocess.PIPE)
	p3 = subprocess.Popen(["tr", "-s", '" "'], stdin=p2.stdout,
stdout=subprocess.PIPE)
	p4 = subprocess.Popen(["cut", "-d ", "-f6"], stdin=p3.stdout, stdout=subprocess.PIPE)
	path = str(p4.communicate()[0])
	print path

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
		return move_files (device_file)

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

It successfully detects the usb, but for some reason when I call move_files to print the usb path, it prints a blank string. Why is this? Thx in advance!

Comments
Nice example of dbus code in python

Did you try df -h | grep YOUR_PTH | tr -s " " | cut -d -f6 in
a console to see if it works ? Also you could probably write

command = "df -h | grep %s | tr -s \" \" | cut -d -f6" % pth
proc = subprocess.Popen(command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
out, err = proc.communicate()

instead of having 4 calls to Popen.

Edited 5 Years Ago by Gribouillis: n/a

Did you try df -h | grep YOUR_PTH | tr -s " " | cut -d -f6 in
a console to see if it works ? Also you could probably write

command = "df -h | grep %s | tr -s \" \" | cut -d -f6" % pth
proc = subprocess.Popen(command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
out, err = proc.communicate()

instead of having 4 calls to Popen.

Thanks for the quick reply! Yes, I have tested that command in the console and it works fine: it gives me the usb path which happens to be /media/03CB-604C. Also, if I take both my code (the move_files func) as well as the code you supplied and run it in the python interactive interpreter, it works perfectly (except there is a newline after printing it, I don't know if this is a big deal). However, when I run the whole thing as a script, even when using your code, it prints an empty string. This leads me to believe there has to be something wrong with the other parts of the code. Any other ideas? Could it be something wrong with my environment? Thx again!

Edited 5 Years Ago by dark3lf: n/a

This article has been dead for over six months. Start a new discussion instead.