Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python 判断目录是否存在

作者:影之哀伤   发布日期:2025-02-12   浏览:78

import os

# 判断目录是否存在
def check_directory_exists(directory_path):
    """
    该函数用于判断给定的目录路径是否存在。

    参数:
    directory_path (str): 要检查的目录路径

    返回:
    bool: 如果目录存在返回 True,否则返回 False
    """
    return os.path.exists(directory_path) and os.path.isdir(directory_path)

# 示例用法
directory_to_check = "/path/to/directory"
if check_directory_exists(directory_to_check):
    print(f"目录 {directory_to_check} 存在")
else:
    print(f"目录 {directory_to_check} 不存在")

解释说明:

  1. 导入模块:我们使用 os 模块中的 os.path.exists()os.path.isdir() 函数来检查路径是否存在并且是否为目录。
  2. 定义函数check_directory_exists 函数接受一个目录路径作为参数,并返回一个布尔值表示该目录是否存在。
  3. 示例用法:通过给定一个具体的目录路径,调用 check_directory_exists 函数并根据返回结果打印相应的信息。

上一篇:python 字符串转16进制

下一篇:python设置全局变量

大家都在看

python时间格式

python ord和chr

python list.pop

python的for i in range

npm config set python

python代码简单

python读取文件夹

python中turtle

python 输出时间

python中list代表什么

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站