Partition names with Python

John Hunter jdhunter at
Mon Jun 30 17:47:54 CEST 2003

>>>>> "Erhan" == Erhan Ekici <erhan at> writes:

    Erhan> Hi, How do I get partition names (i.e. C:\ , D:\ ,E:\ on
    Erhan> windows, hda,hda1 , hda2 on Linux machines) using Python.

There is nothing to do this in the standard library as far as I know,
but it is fairly easy to parse the output of fdisk.  If you are
running as su

  import os

  for line in os.popen('/sbin/fdisk -l').readlines():
      if line.find('/dev/') !=0: continue
      columns = line.split()
      print columns[0].split('/')[-1]

Should do it.  With a little work, you could modify this to work cross
platform if you have fdisk installed on your win32 machine.


More information about the Python-list mailing list