0

I am trying to display partitions of linux using python and i want to do it using APIs. I have just tried the following code to get the output.

import os

f = os.popen ("df -h")
for i in f.readlines():
print "myresult : ", i

3 Answers 3

1

I know this is old, but on linux, /proc/partitions is a special file that lists the partitions, but you probably want /proc/mounts

Sign up to request clarification or add additional context in comments.

Comments

0

df command used for - report file system disk space usage

I think you required fdisk for Partition table manipulator for Linux.

you can use either os.popen or subprocess. just need root user login before running python script.

5 Comments

yeah..that is the problem...i'm not allowed to be the root user :(..so cannot use fdisk
yes for that, you need to run python script using sudo command. read accepted answer in this link : stackoverflow.com/questions/8869574/…
thanks for d link..i tried the code.. it is not giving any error..but it isn't giving any output either..
Because it's required access permission, that you may not applied. copy that same code in py file. then run that same file using this command. 1) cd <to script location> 2) sudo python <your python script name>
one more question..is there any way to display d partitions without being root or using sudo command?? is there any API in python which can do so??
0

The folder /dev contains various devices. From these, devices named like sd[a-z]n (/dev/sda1, for instance) (on old machines with IDE drives hd[a-z]n), (where n-integer) are partitions. You can list using the file system access.

This has some limitations like unusual formatting of the whole drive as partition, but in general should work and also detect non-Linux partitions and with some luck USB sticks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.