开发者

Pythonic way to remove directories from a file listing

I can think of easy ways to accomplish this, but what is the most pythonic way? I get a开发者_开发技巧 file listing for os.listdir, and some may be directories.

I could check through each one in a loop and check if os.isdir returns false, then remove it from the list. What is a pythonic way to write this?


[x for x in os.listdir(...) if not os.path.isdir(os.path.join(..., x))]


files = filter(lambda f: os.path.isfile(os.path.join(path, f)), os.listdir(path))

OR

files = [f for f in os.listdir(path) if not os.path.isdir(os.path.join(path, f))]

Edited: removed bug per Ignacio's answer (added in calls to os.path.join)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜