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

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


qq_笑_17
浏览 4478回答 3
3回答

一只甜甜圈

在os.path模块中有个isfile的方法,该方法可以判断是不是文件,返回True说明是文件,返回False则不是文件,下面的英文是摘自python文档os.path.isfile(path)Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.用法也很简单123import osfilename='/tmp/test.txt'print os.path.isfile(filename) 

慕侠2389804

123456789101112131415161718>>> 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模块里面。参见上文。 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python