Brōtsyorfuzthrāx
Brōtsyorfuzthrāx

Reputation: 4749

How do I find the location where flash drives automatically mount in Python?

How do I find (in Python 3.x) the default location where flash drives automatically mount when plugged in on a computer that I happen to be using at the time? (It could be any of various non-specific Linux distributions and older/new versions. Depending on which one it is, it may mount at such locations as /media/driveLabel, /media/userName/driveLabel, /mnt/driveLabel, etc.)

I was content just assuming /media/driveLabel until Ubuntu updated its default mount location to include the username (so, now I can't use a static location for bookmarked file settings of a portable app I made across my computers, since I use multiple usernames). So, the paths for the bookmarked files need to be updated every time I use a new computer or user. Note that files on the hard drives are also bookmarked (so, those don't need to be changed; they're set not to load if you're not on the right computer for them).

Anyway, now I'm not content just going with /media mounts, if there's a solution here. I would prefer to be able to find this location without having to mount something and find a path's mount location first, if possible (even though that may help me with the problem that sparked the question). It seems like there should be some provision for this, whether in Python, or otherwise.

In other words, I want to be able to know where my flash drive is going to mount (sans the drive label part)—not where it's already mounted.

EDIT: If /media/username/drivelabel is pretty standard for the automatic mounts across all the major distributions that support automatic mounting (the latest versions, at least, as I seem to recall that Ubuntu didn't always include the username), feel free to let me know, as that pretty much answers the question. Or, you could just tell me a list of automatic flash drive mount locations specific to which major distributions. I guess that could work (though I'd have to update it if they changed things).

FYI EDIT: For my problem I'll probably just save the mount location with the bookmark (so my program knows what part of the bookmark path it was when I open it), and replace that in the bookmark path with the new current mount location when a user loads the bookmark.

Upvotes: 0

Views: 830

Answers (3)

n.dury
n.dury

Reputation: 21

Prior to doing whatever you wish to do with the mount path you could ask the user to specify which device they wish to use e.g. /dev/sdb1 /dev/sdb2

It might be possible to use the output of the command

df -h

Which gives following output

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        19G  6.5G   12G  37% /
udev             10M     0   10M   0% /dev
tmpfs           810M  9.0M  801M   2% /run
tmpfs           2.0G   68K  2.0G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           405M  4.0K  405M   1% /run/user/115
tmpfs           405M  8.0K  405M   1% /run/user/1000

running the command

df -h <path to device user wishes to use> | awk '{print $6}'

returns the path to where the device is mounted. After which you've achieved what you're searching for. All of this could be done in your python script as such.

import subprocess
subprocess.call('path to script')

I hope this information get's you closer to your endgoal.

Upvotes: 1

JNevill
JNevill

Reputation: 50034

Some linux systems will not automatically mount devices that are connected to them like Ubuntu, but in versions that do you can poke around at the system to get this information.

Unfortunately, I can only answer this for the Linux CLI as I don't know python well enough

First you can get the UUID of your device using blkid:

$ blkid
/dev/sda1: UUID="4c58b621-59b6-4d05-a785-4671856520de" TYPE="ext4" PARTUUID="7763f5b3-01"
/dev/sda5: UUID="cf64e358-8be2-42f3-972d-70f36469279c" TYPE="swap" PARTUUID="7763f5b3-05"
/dev/sdb1: UUID="05a42322-cd8b-47e2-bc4c-612c7a577626" TYPE="ext3" PARTUUID="5893092a-01"
/dev/sdc1: UUID="64d7548d-689d-407a-9d23-68085476927a" TYPE="ext4" PARTUUID="000ced26-01"
/dev/sdc5: UUID="87cec41f-aa0b-4469-9e92-6ec549ea0b34" TYPE="swap" PARTUUID="000ced26-05"

Determine which UUID is your disk. Once you have that you can use blkid to determine which /dev/sd* your disk is on with:

$ blkid | awk -F":" '$2~"05a42322-cd8b-47e2-bc4c-612c7a577626" {print $1}'
/dev/sdb1

Then you can hit up /proc/mounts where the mount command writes info on mounts:

$ awk '$1~"/dev/sdb1" {print $2}' /proc/mounts
/media/sdb1

Or putting those two together:

$ awk -v device=`blkid | awk -F":" '$2~"05a42322-cd8b-47e2-bc4c-612c7a577626" {print $1}'` '$1~device {print $2}' /proc/mounts
/media/sdb1

Upvotes: 1

BlackHawk
BlackHawk

Reputation: 37

Why don't you use the Udev to force the location by your self, simply you can create a UDEV script that keep listening on the drives insertion and map the inserted USB drive to specific location on the machine

Upvotes: 1

Related Questions