Apr-24-2024, 01:07 PM
(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.mountpointAny 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.")