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 :) ) 6 13135
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-----
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
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()
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Frank Bechmann |
last post by:
yes, that's the simple question:
is there some python standard or 'Python for Windows Extensions'
functionality to iterate or list-return the available drives ('A:',
'B:', 'C:', ...) on a...
|
by: Steve |
last post by:
This post has two parts. First is my feedback on sets. (Hello? Last
summer called, they want their discussion thread back...) Second is some
questions about my implementation of a partition...
|
by: Nick J Chackowsky |
last post by:
Wrote a python script to find the partitions of an integer (a list of
all the ways you can express n as a sum of integers). For example, the
partitions of 5 are
5
4+1
3+2
2+2+1
3+1+1
2+1+1+1...
|
by: RunLevelZero |
last post by:
I need a way to detect hard drives and their partitions... labels would
be nice too... I did some googling but did not find anything all too
useful. This will strictly be on Linux / Unix so any...
|
by: Ned Hart |
last post by:
I'm formatting my mirrored drives and installing a third drive for
raid 5 to add more space. I have backup exec. Can anyone tell me the
best way to backup SQL so I can be sure it will restore...
|
by: Ron Johnson |
last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49
--
-----------------------------------------------------------------
Ron Johnson, Jr....
|
by: BWill |
last post by:
Hi, I'm writing a file browser, but I'm not sure how I could go about
detecting the drives available on windows and linux systems (preferably
using the standard modules if possible). I guess I...
|
by: X~coder |
last post by:
hy all
i don't have a good net connection at home ( it isn't cheap either ) so i can't download alot of stuf but at school we have lets say a good net (15 - 30 KB/s)
so i conviced one of...
|
by: Beshr |
last post by:
Hey all!
Another fast question!
How can I get a list of all available hard disk partitions on the current computer!
I managed to work out one my self but it works only with Windows! Any one...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |