0

I want to run the command df -h to display disk information, But when i run my code nothing is displayed in the terminal, I even tried to do "df -h > out.txt" and then cat out.txt but that didn't work either.

import sys
import os
import subprocess


def main():
    os.system('clear')
    print("Options: \n")
    print("1 - Show disk info")
    print("2 - Quit \n")
    selection = input('> ')
    if selection == 1:
        subprocess.call(['df -h'], shell=True)
    elif selection == 2:
        sys.exit(0)

if __name__ == "__main__":
    main()

1 Answer 1

1

Reading user input with input() returns a string. However, your if/elif statement are comparing the contents of selection with integers, so the comparison will always be False. As a workaround, use this:

selection = int(input('> '))
if selection == 1:
   ...
Sign up to request clarification or add additional context in comments.

1 Comment

no worries, we've all done it. This is why interactive development is so important - running type(selection) or even print(selection) would show you what you need to know.

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.