Get drives and partitions list (Linux)

C

cantabile

Hi, I'd like to get drives and partitions (and their size too) with
python under Linux. So far, I thought of reading /proc/partitions but
maybe i could use fdsik also ?
How would I do that in python ?

Thanks for your help (newbie here :) )
 
J

Jeff Epler

Using /proc/partitions is probably preferable because any user can read
it, not just people who can be trusted with read access to drives, and
because the format of /proc/partitions is probably simpler and more
stable over time.

That said, what you do is
import commands
fdisk_output = commands.getoutput("fdisk -l %s" % partition)
followed by some specialized code to parse the output of 'fdisk -l'
The following code is not at all tested, but might do the trick.

# python parse_fdisk.py
/dev/hda4 blocks=1060290 bootable=False partition_id_string='Linux swap' partition_id=130 start=8451 end=8582
/dev/hda1 blocks=15634048 bootable=True partition_id_string='HPFS/NTFS' partition_id=7 start=1 end=1947
/dev/hda3 blocks=9213277 bootable=False partition_id_string='W95 FAT32 (LBA)' partition_id=12 start=8583 end=9729
/dev/hda2 blocks=52235347 bootable=False partition_id_string='Linux' partition_id=131 start=1948 end=8450

# This source code is placed in the public domain
def parse_fdisk(fdisk_output):
result = {}
for line in fdisk_output.split("\n"):
if not line.startswith("/"): continue
parts = line.split()

inf = {}
if parts[1] == "*":
inf['bootable'] = True
del parts[1]
else:
inf['bootable'] = False

inf['start'] = int(parts[1])
inf['end'] = int(parts[2])
inf['blocks'] = int(parts[3].rstrip("+"))
inf['partition_id'] = int(parts[4], 16)
inf['partition_id_string'] = " ".join(parts[5:])

result[parts[0]] = inf
return result

def main():
import commands
fdisk_output = commands.getoutput("fdisk -l /dev/hda")
for disk, info in parse_fdisk(fdisk_output).items():
print disk, " ".join(["%s=%r" % i for i in info.items()])

if __name__ == '__main__': main()

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCrObVJd01MZaTXX0RAt6FAJ9dDvaJ1L5fxTbvtWCSv7If/eHNaQCdFBcI
fHHR0kcAYQA1Sw5t2BDMMqQ=
=LHbf
-----END PGP SIGNATURE-----
 
P

Peter Hansen

cantabile said:
Hi, I'd like to get drives and partitions (and their size too) with
python under Linux. So far, I thought of reading /proc/partitions but
maybe i could use fdsik also ?
How would I do that in python ?

Somebody else posted a very similar question quite recently. A Google
Groups search would probably lead you to the answers pretty quickly.
(Try "partition proc sfdisk" or something like that.)

My previous answer was to call sfdisk, using os.popen(). Others
suggested reading /proc/partitions. I doubt fdisk (if that's what you
meant) is a good idea.

There is no pure Python solution, so far as I know, probably because
there may not be any standardized way of finding this information.

-Peter
 
C

cantabile

Hi, Jeff

Great help : this works like a charm. I think I can customize it to read
from sfdisk. Do you agree with Peter Hansen (post below) about fdisk ?
 
C

cantabile

Hi, Peter
Thanks for the reply. I'll check popen().
But you said I should not rely on fdisk... Why ? And should I prefer
sfdisk ? Why ?
 
P

Peter Hansen

cantabile said:
Hi, Peter
Thanks for the reply. I'll check popen().
But you said I should not rely on fdisk... Why ? And should I prefer
sfdisk ? Why ?

I was under the impression that fdisk was older and more primitive, but
a quick check shows I'm probably not only wrong, but had it backwards!
(The man page for fdisk says "try parted, then fdisk, then sfdisk"
basically...)

Also, as you saw in Jeff's reply, there's a commands.getoutput()
function that does basically what popen() would do, so just use whatever
seems simplest.

-Peter
 
C

cantabile

Thanks for the answer and help.
Cheers :)

Peter said:
I was under the impression that fdisk was older and more primitive, but
a quick check shows I'm probably not only wrong, but had it backwards!
(The man page for fdisk says "try parted, then fdisk, then sfdisk"
basically...)

Also, as you saw in Jeff's reply, there's a commands.getoutput()
function that does basically what popen() would do, so just use whatever
seems simplest.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top