I am quite new to python and have an issue where I am going to return an
Asked
Active
Viewed 67 times
0
-
3`number = int(q[0])`? :) – CristiFati May 08 '19 at 10:48
-
if I understand correctly you want to have the number as an integer..try to cast it to an int.--> number = int(q[0]) – NemoMeMeliorEst May 08 '19 at 10:48
-
I have tried that and it will jump to my return none value. – NoccoFlow May 08 '19 at 10:51
-
I missed this: rename the name of the argument from ***int*** to something else `def sizeofhome(uid):`, and also where it;s used: `if uid == line[2]:` – CristiFati May 08 '19 at 10:56
-
Cristifati this made the trick, thank you 3000 times <3. 7 hours of work and u solved it in 5min – NoccoFlow May 08 '19 at 11:00
2 Answers
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
-
If it returns None, your if condition is failing. check your if condition. – Abdul Salam May 08 '19 at 10:57
-
-
Other possible debug steps should be: 1. remove try except block and test 2. print line[2] to check what it holds to verify it matches int or not 3. print q before splitting to see if it holds desired value. – Abdul Salam May 08 '19 at 11:01
-
-
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