By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Get drives and partitions list (Linux)

P: n/a
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 :) )
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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)

iD8DBQFCrObVJd01MZaTXX0RAt6FAJ9dDvaJ1L5fxTbvtWCSv7 If/eHNaQCdFBcI
fHHR0kcAYQA1Sw5t2BDMMqQ=
=LHbf
-----END PGP SIGNATURE-----

Jul 19 '05 #2

P: n/a
cantabile wrote:
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
Jul 19 '05 #3

P: n/a
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 ?

Jeff Epler wrote:
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()

Jul 19 '05 #4

P: n/a
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 ?
Peter Hansen wrote:
cantabile wrote:
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

Jul 19 '05 #5

P: n/a
cantabile wrote:
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
Jul 19 '05 #6

P: n/a
Thanks for the answer and help.
Cheers :)

Peter Hansen wrote:
cantabile wrote:
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

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.