Increment number in string
<p>I am trying to get the following output until a certain condition is met.</p>

<pre>
test_1.jpg
test_2.jpg
..
test_50.jpg
</pre>

<p>The solution (if you could remotely call it that) that I have is<pre><code>
fileCount = 0
while (os.path.exists(dstPath)):
   fileCount += 1
   parts = os.path.splitext(dstPath)
   dstPath = "%s_%d%s" % (parts[0], fileCount, parts[1])
</code></pre></p>

<p>however...this produces the following output.</p>

<pre>test_1.jpg
test_1_2.jpg
test_1_2_3.jpg
.....etc</pre>

<p>The Question: How do I get change the number in its current place (without appending numbers to the end)?</p>

<p>Ps. I'm using this for a file renaming tool.</p>

<hr>

<p>UPDATE: Using various ideas below, i've discovered a loop that works</p>

<pre><code>
dstPathfmt = "%s_%d%s"
parts = os.path.splitext(dstPath)
fileCount = 0
while (os.path.exists(dstPath)):
   fileCount += 1
   dstPath = parts[0]+"_%d"%fileCount+parts[1]
</code></pre>
