exists python
时间: 2023-08-25 08:15:39 浏览: 68
exists函数是Python中的一个内置函数,它用于判断指定的文件或目录是否存在。在Python的os模块中,可以使用os.path.exists()函数来判断文件或目录是否存在。如果存在,则返回True,否则返回False。下面是一个使用exists函数的示例代码:
```python
import os
test_path = './test'
if os.path.exists(test_path):
if os.path.isdir(test_path):
print(test_path, 'is dir')
elif os.path.isfile(test_path):
print(test_path, 'is file')
else:
os.mkdir(test_path)
```
这段代码首先判断路径`test_path`是否存在,如果存在,则进一步判断它是一个目录还是一个文件。如果是目录,则输出`test_path is dir`;如果是文件,则输出`test_path is file`。如果路径不存在,则创建一个名为`test`的目录。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python判断文件或目录是否存在(exists)、判断是否为目录(文件夹)、删除、创建目录(文件夹)挺好用](https://blog.csdn.net/weixin_42668334/article/details/115580605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Python,exists判断文件是否存在](https://blog.csdn.net/xunye_dream/article/details/104033219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Python判断文件或目录是否存在(exists)、判断是否为目录(文件夹)、删除、创建目录(文件夹),创建临时...](https://blog.csdn.net/qq_39839807/article/details/104193237)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)