0

I am quite new to python and have an issue where I am going to return an

NoccoFlow
  • 17
  • 3

2 Answers2

0

This should help you!

import subprocess
import re
import pwd

def sizeofhome(int):
   try:
       for line in pwd.getpwall():
           if int == line[2]:
               path = line[5]
               q = subprocess.check_output(["sudo", "du", "-sm", path]).decode()
               q = re.split("\s", q)
               number = int(q[0])
       return(number)
   except:  
       return None
Abdul Salam
  • 522
  • 2
  • 7
  • 26
0

As shown in [SO]: How to convert strings into integers in Python?,
[Python 3.Docs]: Built-in Functions - class int([x]) is used to convert an integer to a string:

>>> int('1586')
1586

But, by naming your function argument int you shadowed the builtin int (above), so the conversion is no longer possible.
Renaming the argument to something else (I'd suggest uid, to be in sync with pwd), the problem should go away:

  • Function header:

    def sizeofhome(uid):
    
  • Function body:

    if uid == line[2]:
    

As a general advice, when encountering such errors, use print (before the line that throws an exception), and also don't ignore the exception thrown.

CristiFati
  • 38,250
  • 9
  • 50
  • 87