import os
# 检查文件或目录是否存在
file_path = "example.txt"
directory_path = "example_directory"
if os.path.exists(file_path):
print(f"{file_path} 存在")
else:
print(f"{file_path} 不存在")
if os.path.exists(directory_path):
print(f"{directory_path} 存在")
else:
print(f"{directory_path} 不存在")
os.path.exists(path) 是 Python 中 os 模块提供的一个函数,用于检查给定路径(可以是文件或目录)是否存在。True;如果路径不存在,则返回 False。上一篇:python 格式化时间
下一篇:python3 venv
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站