Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:12+00:00 2026-05-28T06:52:12+00:00

recently I just started to use python3 and realised that there alot of changes

  • 0

recently I just started to use python3 and realised that there alot of changes made from python2.6. I want to know is there anyway to format the view of the hard disks available in a linux system by using fdisk? in python2.6, it worked something like this;

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():
    fdisk_output = commands.getoutput("fdisk -l")
    for disk, info in parse_fdisk(fdisk_output).items():
        print disk, " ".join(["%s=%r" % i for i in info.items()])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:52:12+00:00Added an answer on May 28, 2026 at 6:52 am

    The commands module has been removed from Python3. You can use the subprocess module instead:

    import subprocess
    import shlex
    import sys
    
    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():
        proc = subprocess.Popen(shlex.split("fdisk -l"),
                                stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        fdisk_output, fdisk_error = proc.communicate()
        fdisk_output = fdisk_output.decode(sys.stdout.encoding)
        for disk, info in parse_fdisk(fdisk_output).items():
            print(disk, " ".join(["%s=%r" % i for i in info.items()]))
    
    main()
    

    No change was made to the parse_fdisk function.
    The only thing that needed changing was the call to commands.getoutput in main().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Mono and just started recently. Is mono program are compiled from
I just recently started using this library (the one from CodePlex), but I ran
I just recently started learning RoR and found that Prototype.js is a default javascript
I've recently started creating a WPF application, and am just hoping that someone can
I have just recently started out programming for Android, and have decided that I
I've recently started work on a new project using PHP5 and want to use
I just recently started diving into Real World Haskell and the book provides some
I have just started C very recently and I have been asked to answer
I am just getting started with Silverlight and have recently added a Silverlight project
I recently started working on a large complex application, and I've just been assigned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.