Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Volume label
#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


Messages In This Thread
Volume label - by DPaul - Apr-22-2024, 03:10 PM
RE: Volume label - by DPaul - Apr-23-2024, 02:23 PM
RE: Volume label - by SandraYokum - Apr-24-2024, 01:07 PM
RE: Volume label - by DPaul - Apr-24-2024, 03:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Ideas for determine the volume of rotating object Lardos 2 2,646 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