Last active
October 24, 2023 06:35
-
-
Save GIJack/d8577e4b52aacce6af5a8ae39eb3b951 to your computer and use it in GitHub Desktop.
get drive/disk information in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import subprocess | |
def byte2str(in_string): | |
'''convert bytes into string''' | |
output = str(in_string.strip()) | |
output = output.lstrip("b") | |
output = output.strip("\'") | |
return output | |
def get_drive_tables(): | |
'''get lsblk data and return it as a structured dict{}''' | |
list_drives_cmd = "lsblk -J -o +LABEL" | |
list_drives = subprocess.Popen(list_drives_cmd, shell=True, stdout=subprocess.PIPE) | |
drive_table, err = list_drives.communicate() | |
drive_table = byte2str(drive_table) | |
drive_table = drive_table.replace('\\n','\n') | |
drive_table = json.loads(drive_table) | |
return drive_table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for that.
I needed a quick listing of UFDs (USD Flash Drives),
and starting with your code, ended up with the code below.
Now I can put a UFD in every port (usually 6~8 ports available),
then run another script that catalogs what's on them (into SQLite)
without any manual mounting etc.
Note: I use prefixes to keep track of data-types, e.g. d for dict, l for list, bs for bytestring.