Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write file to usb with python on linux

Tags:

python

file

usb

I apologise if the question is so stupid but I am new in linux and python. The point is that I need to write a python script which will detect connected usb stick and write a file to it. I use opensuse(but script shoild work with any linux distro. in ideal case). Current version can detect usb and write a file if that usb is mounted. But on my machine usb is mounted only if I browse it in file manager. Until that usb detected as a partion but without mount point. So, how can I force the system to mount usb or how can I write a file using devname or devpath or another information what I can get from hal manager. I can not use "mount" command because it requires a 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()

The output when usb plugged in

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

The output when usb plugged in and mounted

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

Thanks for any help!

like image 615
user1485818 Avatar asked Nov 03 '22 21:11

user1485818


1 Answers

You will have to use mount because the kernel controls mounting partitions within the operating system for security reasons. You can use mount without superuser/administrator privileges. Try something like this:

Make sure that the directory (/mnt/usb1/) you are mounting to already exists, and then

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

There's also something called autofs that does allow automatic mounting:

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

like image 52
Alex W Avatar answered Nov 10 '22 19:11

Alex W