递归os.listdir? [英] Recursive os.listdir?

查看:366
本文介绍了递归os.listdir?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个目录中的所有文件的列表,没有目录。

说,有一个目录〜/具有a.txt,b.txt和d.txt和e的目录c和e中的f.txt的文件我将如何获得一个看起来像 ['/ home / user / files / a.txt','/home/user/files/b.txt','/ home / user /文件/ c / d.txt','/home/user/files/c/e/f.txt']

解决方案

  import os 
[os.path.join(dp,f)for op.walk中的dp,dn,fn(os.path。在fn中为f的expanduser(〜/ files))


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']?

解决方案

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

这篇关于递归os.listdir?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆