PTB*_*BNL 154
os.path.isdir()并os.path.isfile()应该给你你想要的.请参阅:http:
//docs.python.org/library/os.path.html
ret*_*ile 35
正如其他答案所说,os.path.isdir()并且os.path.isfile()是你想要的.但是,您需要记住,这些并不是唯一的两种情况.使用os.path.islink()的符号链接的实例.此外,False如果文件不存在,这些都返回,因此您可能也想要检查os.path.exists().
import os
if os.path.isdir(d):
print "dir"
else:
print "file"
Run Code Online (Sandbox Code Playgroud)
蟒3.4引入的pathlib模块到标准库,它提供了一个面向对象的方法来处理的文件系统的路径.该初步认识方法是.is_file()和.is_dir():
In [1]: from pathlib import Path
In [2]: p = Path('/usr')
In [3]: p.is_file()
Out[3]: False
In [4]: p.is_dir()
Out[4]: True
In [5]: q = p / 'bin' / 'vim'
In [6]: q.is_file()
Out[6]: True
In [7]: q.is_dir()
Out[7]: False
Run Code Online (Sandbox Code Playgroud)
Pathlib也可以通过PyPi上的pathlib2模块在 Python 2.7 上使用.
| 归档时间: |
|
| 查看次数: |
20213 次 |
| 最近记录: |