Python无法识别目录os.path.isdir()

Dan*_*Dan 12 python directory file-io path

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

我得到以下输出:

.svn不是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)
Run Code Online (Sandbox Code Playgroud)

unw*_*ind 38

您需要在检查之前创建完整路径名:

if not os.path.isdir(os.path.join(path, each)):
  ...
Run Code Online (Sandbox Code Playgroud)