I'm working on a picture viewer using python, that executes on startup of X (using LXDE/autostart) and accesses an attached USB drive and starts to display the images it finds. I'm using the current version of Raspbian Wheezy.

I have had to put a delay of 60 seconds on the script (using time.sleep) before it starts looking for images, because if I don't Python reports it can find the USB drive but there are no files present. This means there is quite a delay from reaching the desktop to the picture viewer starting. Is there a clever way in Python of getting round this and not having a delay?



You'd probably want to interface with dbus and listen for usb activity.

There is an example from this StackOverflow post.

There is a tutorial available here, which may give you a better understanding of what dbus is, and how to interface with it in python.


Is the problem the fact that the device isn't getting mounted automatically, and your system is waiting for your X Window System to decide what to do?

  • sudo blkid
    • Identify the blkid of your partition/device
  • sudo vim /etc/fstab
    • UUID=b756693e-XXXX-XXXX-XXXX-XXXXcb3aXXXX /mnt/usb-mount ext4 errors=remount-ro 0 1
| improve this answer | |

You can list your USB drive mounted or not via Python code:

import os
partitionsFile = open("/proc/partitions")
lines = partitionsFile.readlines()[2:]#Skips the header lines
for line in lines:
    words = [x.strip() for x in line.split()]
    minorNumber = int(words[1])
    deviceName = words[3]
    if minorNumber % 16 == 0:
        path = "/sys/class/block/" + deviceName
        if os.path.islink(path):
            if os.path.realpath(path).find("/usb") > 0:
                print "/dev/%s" % deviceName

Then if your device mounted you can do what ever you want with your code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.