When you call listdir
, are you trying to print its return value?
print listdir(path)
listdir
doesn't return a value, so if you did that the print
statement will print None
. Leave out that print
:
listdir(path)
Question
I have this piece of code:
import os
def listdir(path):
print(os.listdir(path))
print '\n'.join(os.listdir(path))
which returns
['.idea', 'commands', 'testfile.py', '__pycache__']
.idea
commands
testfile.py
__pycache__
None
I do not understand why I get None value on the last line? Thank for any advice.
Solution
When you call listdir
, are you trying to print its return value?
print listdir(path)
listdir
doesn't return a value, so if you did that the print
statement will print None
. Leave out that print
:
listdir(path)
OTHER TIPS
If there is no return statement, the function implicitly returns None
.
>>> def func():
... 2013 # no value is being returned
...
>>> func()
>>> func() is None
True
>>> def func():
... return 2013
...
>>> func()
2013