3 回答
![?](http://img1.sycdn.imooc.com/56fb3e3d0001a10301000100-100-100.jpg)
TA贡献1836条经验 获得超5个赞
在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.
用法也很简单
1 2 3 | import os filename='/tmp/test.txt' print os.path.isfile(filename) |
![?](http://img1.sycdn.imooc.com/533e4c5600017c5b02010200-100-100.jpg)
TA贡献1719条经验 获得超6个赞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> 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模块里面。参见上文。
添加回答
举报