在Python中,你可以使用`os`模块或`pathlib`模块来判断目录是否存在。以下是两种方法的示例:
使用 `os` 模块
import osdef check_path_exists(path):if os.path.exists(path):if os.path.isdir(path):print(f"{path} 是一个目录。")else:print(f"{path} 是一个文件。")else:print(f"{path} 不存在。")示例check_path_exists("/path/to/directory") 替换为你的目录路径
使用 `pathlib` 模块

from pathlib import Pathdef check_path_exists(path):p = Path(path)if p.exists():if p.is_dir():print(f"{path} 是一个目录。")else:print(f"{path} 是一个文件。")else:print(f"{path} 不存在。")示例check_path_exists("/path/to/directory") 替换为你的目录路径
在这两种方法中,`os.path.exists` 和 `pathlib.Path.exists` 函数都可以用来检查路径是否存在,而 `os.path.isdir` 和 `pathlib.Path.is_dir` 则分别用来检查路径是否为目录。
请选择适合你需求的方法,并替换示例中的路径为你想要检查的实际路径
