特定のディレクトリ内のすべてのファイルを列挙しようとしています(Linuxでは「find。」、Windowsでは「dir / s / b」など)。
私は次のネストされたリスト内包表記を思いついた:
from os import walk
from os.path import join
root = r'c:\windows' #choose any folder here
allfiles = [join(root,f) for f in files for root,dirs,files in walk(root)]
残念ながら、最後の式については、次のようになります。
NameError: name 'files' is not defined
この質問に関連して、(機能しているものの)ネストされたリスト内包表記の構文を理解できません。