擅长:python、mysql、java
<p>与<a href="http://docs.python.org/3.3/library/os.path.html?highlight=os.path#os.path.isfile" rel="noreferrer">^{<cd1>}</a>不同,<a href="http://docs.python.org/3.3/library/os.path.html?highlight=os.path#os.path.exists" rel="noreferrer">^{<cd2>}</a>将返回目录的<code>True</code>。<br/>
因此,根据您是否只需要普通文件或目录,您将使用<code>isfile()</code>或<code>exists()</code>。这是一个简单的REPL输出。</p>
<pre><code>>>> print os.path.isfile("/etc/password.txt")
True
>>> print os.path.isfile("/etc")
False
>>> print os.path.isfile("/does/not/exist")
False
>>> print os.path.exists("/etc/password.txt")
True
>>> print os.path.exists("/etc")
True
>>> print os.path.exists("/does/not/exist")
False
</code></pre>