Recursive os.listdir? [duplicate]

假如想象 提交于 2019-12-12 10:29:30

问题


I'd like to get a list of all files in a directory recursively, with no directories.

Say there's a directory ~/files with "a.txt", "b.txt", and a directory "c" with "d.txt" and "e" inside it, and "f.txt" inside e. How would I go about getting a list that looks like ['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt']?


回答1:


import os
[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("~/files")) for f in fn]


来源:https://stackoverflow.com/questions/19309667/recursive-os-listdir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!