python 判断文件是文件还是文件夹

2025-03-12 04:23:32
推荐回答(1个)
回答(1):

>>> import os.path
>>> help(os.path.isfile)
Help on function isfile in module genericpath:

isfile(path)
    Test whether a path is a regular file

>>> help(os.path.isdir)
Help on function isdir in module genericpath:

isdir(s)
    Return true if the pathname refers to an existing directory.

>>> os.path.isfile('/etc/hostname')
True
>>> os.path.isdir('/etc/hostname')
False
>>>

两个函数都在os.path模块里面。参见上文。