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.

有帮助吗?

解决方案

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)

其他提示

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top