Partition names with Python

Bengt Richter bokr at oz.net
Mon Jun 30 22:45:15 EDT 2003


On Mon, 30 Jun 2003 10:47:54 -0500, John Hunter <jdhunter at ace.bsd.uchicago.edu> wrote:

>>>>>> "Erhan" == Erhan Ekici <erhan at uzem.itu.edu.tr> 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
(I always get nervous ;-)
>
>  #!/your/path/to/python2.2
>  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.
>
Well, on my linux box (slackware) fdisk is not even on the $PATH for a non-root user.
(I think that might be good ;-) And if you try /sbin/fdisk -l as ordinary user
you'll get access denial ("can't open") on /dev/hdxxx etc.

You might want to try something like

    import os
    print os.popen('mount').read()

and see if parsing that would be useful. Or if you want to look at
non-mounted possibilities, perhaps

    print open('/etc/fstab').read()

will show something interesting. There are man entries for both mount and fstab, but
it's a lot of reading ;-/

Neither of the latter require su, AFAIK.

Regards,
Bengt Richter




More information about the Python-list mailing list