为什么我不能做这样的事情:
files = [file for file in ['default.txt'].append(sys.argv[1:]) if os.path.exists(file)]
最佳答案
list.append
在 Python 中不返回任何内容:
>>> l = [1, 2, 3]
>>> k = l.append(5)
>>> k
>>> k is None
True
你可能想要这个:
>>> k = [1, 2, 3] + [5]
>>> k
[1, 2, 3, 5]
>>>
或者,在您的代码中:
files = [file for file in ['default.txt'] + sys.argv[1:] if os.path.exists(file)]
关于Python - 迭代 list.append 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5390352/