import os
# 定义一个函数来判断目录是否存在
def check_directory_exists(directory_path):
# 使用 os.path.isdir() 方法检查路径是否为目录且存在
if os.path.isdir(directory_path):
print(f"目录 {directory_path} 存在。")
return True
else:
print(f"目录 {directory_path} 不存在。")
return False
# 示例用法
directory_to_check = "/path/to/your/directory"
check_directory_exists(directory_to_check)
os 模块:os 模块提供了与操作系统交互的功能,包括文件和目录操作。check_directory_exists:该函数接受一个参数 directory_path,表示要检查的目录路径。os.path.isdir() 方法:此方法用于检查给定路径是否为目录且存在。如果是,则返回 True,否则返回 False。directory_to_check 变量为你要检查的目录路径,并调用 check_directory_exists 函数来验证目录是否存在。如果你需要更详细的帮助或有其他问题,请告诉我!
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站