Python中判断所给路径是文件还是目录

在python中有时不知道所给的路径是目录还是文件,可以用下面方法判断所给的路径是目录还文件,或者其他类型。

def get_path_type(path):
    if os.path.isdir(path):
        return 'dir'
    if os.path.isfile(path):
        return 'file'
    return 'other'

你可能感兴趣的:(Python中判断所给路径是文件还是目录)