1. Home
  2. Docs
  3. Python
  4. 檔案
  5. 檔案或文件夾是否存在

檔案或文件夾是否存在

    from pathlib import Path
    
    my_file = Path("/path/to/file")
    if my_file.is_file():
        # file exists
    if my_file.is_dir():
        # directory exists
    if my_file.exists():
        # path exists
    try:
        my_abs_path = my_file.resolve(strict=True)
    except FileNotFoundError:
        # doesn't exist
    else:
        # exists