How to find if directory exists in Python
在Python的os
模块中,有没有办法找到一个目录是否存在,如:
>>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode
True/False
You're looking for os.path.isdir
, or os.path.exists
if you don't care whether it's a file or a directory.
Example:
import os
print(os.path.isdir("/home/el"))
print(os.path.exists("/home/el/myfile.txt"))
So close! os.path.isdir
returns True
if you pass in the name of a directory that currently exists. If it doesn't exist or it's not a directory, then it returns False
.
是的,使用os.path.exists()
。
上一篇: 目的'如果
下一篇: 如何查找Python中是否存在目录