The logic in main() does not seem to hit the else condition - why

42 Views Asked by At

I have a function below, that looks at a path, and determines how much of the disk space is used.

def check_disk_space():
    import os
    cmdparts = ["echo $(df --output=pcent ", ") | tr -d 'Use% '"]
                check_used_disk_space_cmd =  cmdparts[0] + "a/path" + cmdparts[1]
    os.system(check_used_disk_space_cmd)

def main():
    used_disk_space = check_disk_space()
    print type( used_disk_space )
    if  int(used_disk_space) > 80:
        print "need more"
    else:
        print "plennnty!"
        print type( used_disk_space )
main()

check_disk_space() is returning 85.

Update: it appears check_disk_space() is creating a NoneType object? I'm getting this error: TypeError: int() argument must be a string or a number, not 'NoneType'

1

There are 1 best solutions below

0
On BEST ANSWER

There are couple of things I changed in your code.

  1. You are not returning any value from your function
  2. import os can be moved to top of the file

NOTE: I have added braces to print statement.

import os

def check_disk_space():
    """
    check_disk_space() checks the available space of a specified path
    """
    cmdparts = ["echo $(df --output=pcent ", ") | tr -d 'Use% '"]
    check_used_disk_space_cmd =  cmdparts[0] + "C:/Users/jgosalia/Desktop" + cmdparts[1]
    return os.system(check_used_disk_space_cmd)

def main():
    space = check_disk_space()
    print("Space : " + str(space))
    if space > 95:
        print ("need more")
    else:
        print ("plennnty!")

main()

Sample Run 1 :

===== RESTART: C:/filesOperation.py =====
Space : 255
need more

Changed if condition from > to < to check else condition and it works.

Sample Run 2 :

===== RESTART: C:/filesOperation.py =====
Space : 255
plennnty!