Python - 迭代list.append的结果

Mar*_*lík 2 python list

为什么我不能这样做:

files = [file for file in ['default.txt'].append(sys.argv[1:]) if os.path.exists(file)]
Run Code Online (Sandbox Code Playgroud)

Eli*_*sky 10

list.append 不会在Python中返回任何内容:

>>> l = [1, 2, 3]
>>> k = l.append(5)
>>> k
>>> k is None
True
Run Code Online (Sandbox Code Playgroud)

你可能想要这个:

>>> k = [1, 2, 3] + [5]
>>> k
[1, 2, 3, 5]
>>> 
Run Code Online (Sandbox Code Playgroud)

或者,在您的代码中:

files = [file for file in ['default.txt'] + sys.argv[1:] if os.path.exists(file)]
Run Code Online (Sandbox Code Playgroud)