获得子目录 [英] Gettings subdirectories

查看:97
本文介绍了获得子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

如何获取给定目录的所有子目录? os.listdir()给了我所有条目的
我发现没有办法判断对象是文件还是

目录。
< br $> b $ b谢谢,


Florian

解决方案

Florian Lindner启发我们:< blockquote class =post_quotes>如何获取给定目录的所有子目录?
os.listdir()给我所有条目,我发现没有办法判断对象是否是一个文件或目录。




为什么,你的os.path.isdir()函数不起作用吗?


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa


这有点矫枉过正,但考虑使用os.walk。


root,dirnames,filenames = os.walk(r" C:\")。next()#(在实际代码中,

你想在这里捕获异常)

print dirnames


Florian Lindner写道:

您好,
如何获取给定目录的所有子目录? os.listdir()给了我所有条目,我发现没有办法判断对象是文件还是
目录。

谢谢,
Florian




>> root,dirnames,filenames = os.walk(r" C:\")。next()


哇。这是如何运作的?请指出我可以阅读的内容。我不会在os.walk上看到



这很酷。


谢谢,


Rick


Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I''ve found no way to tell if an object is a file or a
directory.

Thanks,

Florian

解决方案

Florian Lindner enlightened us with:

how can I get all subdirectories of a given directories?
os.listdir() gives me all entries and I''ve found no way to tell if
an object is a file or a directory.



Why, doesn''t your os.path.isdir() function work?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


It''s a bit overkill, but consider using os.walk.

root, dirnames, filenames = os.walk(r"C:\").next() # (in real code,
you''d want to catch exceptions here)
print dirnames

Florian Lindner wrote:

Hello,
how can I get all subdirectories of a given directories? os.listdir() gives
me all entries and I''ve found no way to tell if an object is a file or a
directory.

Thanks,

Florian




>> root, dirnames, filenames = os.walk(r"C:\").next()

Wow. How does that work? Just point me to where I can read about it. I
don''t see it under os.walk.

That''s cool.

Thanks,

Rick


这篇关于获得子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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