Write file in usb using python on linux

Sorry if the question is so stupid, but I'm new to linux and python. The thing is, I need to write a python script that will detect the connected USB drive and write the file to it. I use opensuse (but the script shoild works with any linux distribution in the ideal case). The current version can detect usb and write the file if it is installed. But on my computer, usb is mounted only if I view it in the file manager. Until this usb is detected as partial, but without a mount point. So, how can I get the system to mount usb or how to write a file using devname or devpath or other information that I can get from hal manager. I cannot use the mount command because it requires root permission

Simplified source code:

import dbus, gobject, os

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.added)

    def show(self, name, udi):
        d_object = self.bus.get_object('org.freedesktop.Hal', udi)
        d_interface = dbus.Interface(d_object,'org.freedesktop.Hal.Device')

        if d_interface.QueryCapability("volume"):
            print name
            props = [ "block.device", "volume.label",  "volume.is_mounted", "volume.mount_point", "volume.size"]
            for p in props:
                print '\t', p, " = ",
                try:
                    print d_interface.GetProperty(p)
                except:
                    print "Fail"

    def added(self, udi): 
        self.show("DeviceAdded", udi)
        self.bus.add_signal_receiver(self.property_modified,
            "PropertyModified",
            "org.freedesktop.Hal.Device", 
            "org.freedesktop.Hal",
            udi,
            path_keyword = "sending_device" )

    def property_modified(self, numupdates, updates, sending_device = None): 
        self.show("PropertyModified", sending_device) 



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

Exit when usb is connected to

DeviceAdded
block.device  =  /dev/sdb1
volume.label  =  @
volume.is_mounted  =  0
volume.mount_point  =  
volume.size  =  4009722368

, usb

    block.device  =  /dev/sdb1
volume.label  =  @
volume.is_mounted  =  1
volume.mount_point  =  /media/@
volume.size  =  4009722368

!

+5
2

mount, โ€‹โ€‹ . mount /. - :

, (/mnt/usb1/), , ,

mount /dev/sdb1 /mnt/usb1 -o noauto,users,rw,umask=0

- autofs, :

http://linuxconfig.org/automatically-mount-usb-external-drive-with-autofs

+2

HAL , , UDisks USB. FilesystemMount FilesystemUnmount, (https://gist.github.com/3037221) (http://hal.freedesktop.org/docs/udisks/Device. HTML # Device.FilesystemMount).

0

All Articles