os.path.exists与os.path.isdir之间的优缺点 [英] pros and cons between os.path.exists vs os.path.isdir

查看:571
本文介绍了os.path.exists与os.path.isdir之间的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查目录是否存在,但是我发现我使用的是 os.path.exists 而不是 os.path .isdir 。两者都很好,但是我很好奇使用 isdir 代替 有什么优势。

I'm checking to see if a directory exists, but I noticed I'm using os.path.exists instead of os.path.isdir. Both work just fine, but I'm curious as to what the advantages are for using isdir instead of exists.

推荐答案

os.path.exists 也将返回 True 是否存在具有该名称的常规文件。

os.path.exists will also return True if there's a regular file with that name.

os.path.isdir 仅会返回 True (如果该路径存在并且是目录),或指向目录的符号链接。

os.path.isdir will only return True if that path exists and is a directory, or a symbolic link to a directory.

这篇关于os.path.exists与os.path.isdir之间的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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