【问题标题】:Avoid including the current and parent directories [duplicate]避免包括当前目录和父目录[重复]
【发布时间】:2018-06-25 11:34:10
【问题描述】:

所以,我正在循环浏览我的文件和文件夹,如下所示:

for root, dirs, files in os.walk(img):

我们如何避免在此类搜索中包含current (.) 和parent (..) 目录?

谢谢。

【问题讨论】:

  • 不确定您的问题是否足够清楚,实际条目 ... 不会出现在 os.walk 结果中。

标签: python for-loop directory parent


【解决方案1】:

os.walk() 不会浏览您的父目录(我假设您的意思是img 的父目录),因此您无需担心。如果只想跳过分配给img的当前目录,很简单:

for root, dirs, files in os.walk(img):
    if root == img: continue
    # your code for the children dirs and files here #

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2015-08-22
    • 2010-09-24
    • 2016-07-06
    • 2011-07-05
    • 2011-12-31
    相关资源
    最近更新 更多