import os
def check_file_exists(file_path):
"""
检查文件是否存在
参数:
file_path (str): 文件的路径
返回:
bool: 如果文件存在返回 True,否则返回 False
"""
return os.path.exists(file_path)
# 示例用法
file_path = 'example.txt'
if check_file_exists(file_path):
print(f"文件 '{file_path}' 存在")
else:
print(f"文件 '{file_path}' 不存在")
os
模块中的 path.exists
方法来检查文件是否存在。check_file_exists
函数接收一个文件路径作为参数,并返回布尔值表示文件是否存在。check_file_exists
函数,根据返回结果打印相应的信息。下一篇:python中chr函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站