-2

I am trying to make a script to read an SD card's UUID, but I didn't find anything helpful. I know I can read it in the terminal with the command:

sudo blkid

But is there any way that I can read it through a Python script?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jaxsss
  • 41
  • 3
  • What research have you done? Questions like "*Is X possible*" aren't great fits for Stack Overflow, as most always the answer is "yes, with enough time, money, and resources." Instead, edit your question to show the community what research you've done, what code you've written, and where you're getting stuck in those efforts (why they don't meet your requirements), in accordance with [ask]. Have you reviewed the [`man` page for `blkid`](https://linux.die.net/man/8/blkid)? It specifically mentions it's simply a wrapper for [`libblkid`](https://linux.die.net/man/3/libblkid). (1/2) – esqew Jun 10 '21 at 15:16
  • ... as such, you should be able to [compile bindings for it for use in Python](https://stackoverflow.com/questions/145270/calling-c-c-from-python). (2/2) – esqew Jun 10 '21 at 15:17

1 Answers1

0

This can be easily accomplished using the subprocess library

import subprocess
command = "sudo blkid"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print(output)

Giving you:

/dev/sdb1: LABEL="ESP" UUID="<UUID>" TYPE="vfat" PARTLABEL="EFI system partition" PARTUUID="<PARTUUID>"
/dev/sdb3: UUID="<UUID>" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="<PARTUUID>"

Had to cover my UUID's just in case :)

Keep in mind that you will be asked for root privileges the first time you run the script through bash, but the second time will be smooth as needed, or just run it as root without sudo

Hotkuk
  • 106
  • 9