python检查文件夹是否存在
时间: 2023-04-06 19:01:01 浏览: 48
您好,可以使用以下代码来检查文件夹是否存在:
```python
import os
folder_path = "your/folder/path"
if os.path.exists(folder_path):
print("文件夹存在")
else:
print("文件夹不存在")
```
希望能对您有所帮助。
相关问题
python 判断文件夹是否存在
在 Python 中,可以使用 `os.path.exists()` 函数来判断文件夹是否存在。以下是一个例子:
```python
import os
folder_path = '/path/to/folder'
if os.path.exists(folder_path):
print(f'The folder {folder_path} exists.')
else:
print(f'The folder {folder_path} does not exist.')
```
在上面的例子中,将文件夹路径存储在变量 `folder_path` 中,然后使用 `os.path.exists()` 函数来检查该文件夹是否存在。如果文件夹存在,则输出一条消息,否则输出另一条消息。
python检查文件夹路径是否存在,不存在就创建
你可以使用Python中的os模块来检查路径是否存在并创建文件夹。以下是一个示例代码:
```python
import os
# 检查文件夹是否存在,不存在则创建
folder_path = '/path/to/folder'
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# 后续操作
# ...
```
在这个例子中,`os.path.exists()`函数用于检查路径是否存在,如果不存在,则使用`os.makedirs()`函数创建路径。
你需要将`/path/to/folder`替换为你想要检查和创建的文件夹路径。