擅长:python、mysql、java
<p>你已经很接近了。适应<a href="https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python">from this answer</a>:</p>
<pre><code>import os, errno
def mkdir(path, overwrite=False):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
if not overwrite:
print "path '%s' already exists" % path # overwrite == False and we've hit a directory that exists
else: raise
</code></pre>
<p>我不明白为什么您需要在<code>try</code>块上使用<code>else</code>。在</p>