os.path.exists
时间: 2023-12-09 14:01:51 浏览: 93
Python常用模块os.path之文件及路径操作方法
os.path.exists() is a method in Python's os module that checks whether a path exists or not. It returns True if the path exists and False if it does not exist. The path can be a file or a directory.
For example, the following code checks whether a file named "example.txt" exists in the current directory:
```python
import os
if os.path.exists("example.txt"):
print("The file exists")
else:
print("The file does not exist")
```
If the file "example.txt" exists in the current directory, the output will be "The file exists". Otherwise, the output will be "The file does not exist".
阅读全文