简体   繁体   English

如何区分 python 中的目录和文件路径?

[英]How can I differentiate between a directory and a file path in python?

I have written the following source code to test whether a string is a directory or a file.我编写了以下源代码来测试string是目录还是文件。

import platform
from PathValidation import PathValidation # https://stackoverflow.com/a/34102855/159072


class SystemUtils:
    @staticmethod
    def is_file(path_string: str) -> bool:
        return not SystemUtils.is_directory(path_string)        ​
   ​
    ​@staticmethod
​    def is_directory(path_string: str) -> bool:
​       is_win_os = SystemUtils.is_windows()
​           
    ​    if is_win_os is True:
       ​     path_string = SystemUtils.to_windows_path(path_string)
    ​    else:
            ​path_string = SystemUtils.to_unix_path(path_string)
    ​
    ​    valid = PathValidation.is_pathname_valid(path_string)
    ​
    ​    if valid is False:
           ​return False
    ​
    ​    if is_win_os is True:
       ​     if path_string.startswith("\\") or path_string.endswith("\\"):
           ​     return True
    ​    else:
       ​     if path_string.startswith("/") or path_string.endswith("/"):
           ​     return True
    ​    # END of outer if
    ​    return False

   ​ @staticmethod
   ​ def to_windows_path(path_string) -> str:
       ​ path_string = path_string.replace("/", "\\")
        ​return path_string

   ​ @staticmethod
   ​ def to_unix_path(path_string) -> str:
        ​path_string = path_string.replace("\\", "/")
        ​return path_string

   ​ @staticmethod
   ​ def is_windows() -> bool:
       ​ system_type = platform.system()
       ​ if system_type == "Windows":
            ​return True
        ​else:
            ​return False

   ​ @staticmethod
   ​ def is_linux() -> bool:
        ​system_type = platform.system()
        ​if system_type == "Linux":
           ​ return True
       ​ else:
           ​ return False

    ​@staticmethod
    ​def is_darwin() -> bool:
        ​system_type = platform.system()
       ​ if system_type == "Darwin":
            ​return True
       ​ else:
           ​ return False

Can anyone suggest any better technique?任何人都可以提出任何更好的技术吗?

use pathlib.Path , in the std library.在 std 库中使用pathlib.Path

from pathlib import Path

path_string = "xxx"

pa = Path(path_string)

print(f"{pa.is_file()}")
print(f"{pa.is_dir()}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python的路径目录中获取.img文件? - How can I get the .img file from the path directory in python? 如何通过查找区分用户 - How can I differentiate between users with lookup 区分文件名和文件路径 - differentiate between file name and file path 如何在ly,python中使用lex区分任何单词和特定单词? - How can I differentiate between any word and a specific word with lex in ply, python? Python - dir() - 如何区分函数/方法和简单属性? - Python - dir() - how can I differentiate between functions/method and simple attributes? Python:如何区分同一类的两个不同对象的两个变量? - Python: how can I differentiate between two variables of two different objects of the same class? 如何在python中获取当前.py文件的更高级别目录的路径? - How can I get the path of the higher level directory of the current .py file in python? Python-lambda aws:[Errno 2]没有这样的文件或目录:我如何解析真实路径 - Python-lambda aws: [Errno 2] No such file or directory: How can i parse real path 如何基于绝对路径访问完全不同目录中的文件(Python) - How can I access a file in a completely different directory based on absolute path (Python) 我们如何区分解释器和 ide 和空闲? 在 python 中? - How we can differentiate between interpreter and ide and idle?in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM