Frage

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.

War es hilfreich?

Lösung

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)

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top