문제

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