Python Forum
Volume label - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html)
+--- Forum: Bar (https://python-forum.io/forum-27.html)
+--- Thread: Volume label (/thread-42008.html)



Volume label - DPaul - Apr-22-2024

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


RE: Volume label - DPaul - Apr-23-2024

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


RE: Volume label - SandraYokum - Apr-24-2024

(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.")



RE: Volume label - DPaul - Apr-24-2024

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