擅长:python、mysql、java
<p>同样数量的陈述,但可能稍微可读。使用<a href="https://docs.python.org/2/library/itertools.html#itertools.count" rel="nofollow"><strong>^{<cd1>}</strong></a>:</p>
<pre><code>from itertools import count
def next_available(base):
for ext in count(start=1):
name = '{}.{:03d}'.format(base, ext)
if not os.path.exists(name):
return name
</code></pre>