Python 无法识别目录 os.path.isdir() [英] Python not recognising directories os.path.isdir()
本文介绍了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 不是目录
任何想法将不胜感激.
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屋!
查看全文