Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Volume label
#1
Hi,
I would like to do the following:
When I start my search program for prayer cards,
the system looks immediately for availability of the data on the server.
That works fine. If the server is not available, nothing happens, user has to wait.

But I can attach an external SSD to the search PC, that also has some data.
If the server is not available but the SSD is, i would like the program to find the SSD.
What is my question?
I cannot predict what disk letter the ssd will be at : F:\, G:\ ... ?
What i can do is make sure it has a specific volume label : eg. 'myVolume5'.
What I need is a piece of code that searches for the volume label and tells me what the drive letter is.
I was hoping that this would do the trick, but it doesn't:
if psutil.disk_partitions()[0].opts[0] == label:
                return partition.mountpoint 
Any ideas ?
thx,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#2
The answer is hidden in python 3.12.
New os.listdrives() function works fine.
But where are the drive labels ?
I can find c:\, d:\, e:\ etc. , but fail to associate them with the volume labels.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Apr-22-2024, 03:10 PM)DPaul Wrote: Hi,
I would like to do the following:
When I start my search program for prayer cards,
the system looks immediately for availability of the data on the server.
That works fine. If the server is not available, nothing happens, user has to wait.

But I can attach an external SSD to the search PC, that also has some data.
If the server is not available but the SSD is, i would like the program to find the SSD.
What is my question?
I cannot predict what disk letter the ssd will be at : F:\, G:\ ... ?
What i can do is make sure it has a specific volume label : eg. 'myVolume5'.
What I need is a piece of code that searches for the volume label and tells me what the drive letter is.
I was hoping that this would do the trick, but it doesn't:
if psutil.disk_partitions()[0].opts[0] == label:
                return partition.mountpoint 
Any ideas ?
thx,
Paul

import psutil
import os

def find_drive_by_label(target_label):
    # Iterate over all mounted disk partitions
    for partition in psutil.disk_partitions():
        if 'fixed' in partition.opts or 'removable' in partition.opts:
            # Try to access the drive and get its label
            try:
                # This will work on Windows, adjusting the approach for other OS might be necessary
                drive = partition.device
                info = os.statvfs(drive)
                volume_label = os.path.basename(os.path.normpath(drive))
                # Compare the volume label
                if volume_label == target_label:
                    return drive
            except (PermissionError, FileNotFoundError):
                # Permission error or the drive may not be accessible
                continue
    return None

# Usage
target_label = 'myVolume5'
drive_letter = find_drive_by_label(target_label)
if drive_letter:
    print(f"Found drive at: {drive_letter}")
else:
    print("Drive with specified label not found.")
Reply
#4
Ok, close, but I get an error:
Output:
AttributeError: module 'os' has no attribute 'statvfs'
Even in Python 3.12 it does not.
I see os.statvfs_result(), but that takes many more arguments.
In python 3.12 this works:
for drive in  os.listdrives():
but only for drive letters, I don't seem to be able to get the volume labels.
But if you have the drive letters, you can think of many workarounds to get the desired result.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ideas for determine the volume of rotating object Lardos 2 2,545 Apr-05-2019, 04:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020