Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     R                RAW    Partition    931 GB  Healthy



i am verifying the drive letter with Volume number

but i am not geting exactly R letter from remaiing all i am geting


what i am doing mistake in below code

please let me know

What I have tried:

def verify_drive_letter1(volume_number,drive_letter):
    verify_drive_command=["diskpart",'/s',"verify_list.txt"]
    with open("verify_list.txt","w") as f:
        f.write("select disk {}\n".format(volume_number))
        f.write("detail disk\n")
    try:
        result=subprocess.run(verify_drive_command,capture_output=True,text=True)
        output=result.stdout
        print(output)

    except Exception as e:
        print("faild to list Drive Letter ",e)
        return None

    for line in output.split('\n'):
        if line.startswith('  Volume '):
            found_drive_letter = line.split("\n")[-1]   
            get_drive_letter= found_drive_letter.split()[2][1:]
            if f"{drive_letter}:" in get_drive_letter and volume_number in line:
                return True
Posted
Updated 17-Feb-23 6:04am

Firstly, make sure that you run your script as Administrator as Diskpart requires administrative privileges.

The following should return Driveletter R -

try:
        result = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        raise Exception(f"Failed to get drive letter: {e.output.decode('utf-8')}")

    result = result.decode('utf-8')
    for line in result.split('\n'):
        if volume_name in line:
            return line.split()[1]

    raise Exception(f"Failed to get drive letter: Volume '{volume_name}' not found")
    

//An example to get the drive letter of a volume named 'MyDisk'
try:
    drive_letter = get_drive_letter('MyDisk')
    print(f"Drive letter: {drive_letter}")
except Exception as e:
    print(f"Error: {e}")
 
Share this answer
 
v2
Comments
HelpMewithCode 17-Feb-23 9:20am    
Above code run diskpart in admin mode only


i am getting disknumber also and volume also

but i am not able to separate the word called R
Andre Oosthuizen 17-Feb-23 9:41am    
I am glad this helped. Your comment is a little confusing as R is a letter, how can you separate it as a word?
This should work:
Python
if line.startswith('  Volume '):
    get_drive_letter = line.split()[2]
 
Share this answer
 
Comments
HelpMewithCode 27-Feb-23 9:47am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900