Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想递归地获取目录中所有文件的列表,没有目录。
假设有一个目录~/files,其中包含“a.txt”、“b.txt”,以及一个目录“c”,其中包含“d.txt”和“e”,而“f.txt”则包含在 e 中。我将如何获得一个看起来像的列表['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt']?
~/files
['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt']
import os [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("~/files")) for f in fn]