如何查找Python中是否存在目录 [英] How to find if directory exists in Python

查看:129
本文介绍了如何查找Python中是否存在目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中的 os 模块中,有没有办法找到目录是否存在,如:

 >>> os.direxists(os.path.join(os.getcwd()),'new_folder'))#在伪代码
True / False


解决方案

您正在寻找 os.path.isdir ,或 os.path.exists ,如果您不在乎它是文件还是目录。



示例:

  import os 
print(os .path.isdir(/ home / el))
print(os.path.exists(/ home / el / myfile.txt))
pre>

In the os module in Python, is there a way to find if a directory exists, something like:

>>> 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"))

这篇关于如何查找Python中是否存在目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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