Python不识别目录os.path.isdir() [英] Python not recognising directories os.path.isdir()

查看:754
本文介绍了Python不识别目录os.path.isdir()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Python代码来删除目录中的文件。
由于某种原因,我的.svn目录不被识别为目录。

I have the following Python code to remove files in a directory. For some reason my .svn directories are not being recognised as directories.

我得到以下输出:


.svn不是目录

.svn not a dir

任何想法都不胜感激。

def rmfiles(path, pattern):
    pattern = re.compile(pattern)
    for each in os.listdir(path):
        if os.path.isdir(each) != True:
            print(each +  " not a dir")
            if pattern.search(each):
                name = os.path.join(path, each)
                os.remove(name)


推荐答案

您需要先创建完整的路径名,然后再检查:

You need to create the full path name before checking:

if not os.path.isdir(os.path.join(path, each)):
  ...

这篇关于Python不识别目录os.path.isdir()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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