3

I am trying to get the following output until a certain condition is met.

test_1.jpg
test_2.jpg
..
test_50.jpg

The solution (if you could remotely call it that) that I have is


fileCount = 0
while (os.path.exists(dstPath)):
   fileCount += 1
   parts = os.path.splitext(dstPath)
   dstPath = "%s_%d%s" % (parts[0], fileCount, parts[1])

however...this produces the following output.

test_1.jpg
test_1_2.jpg
test_1_2_3.jpg
.....etc

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

Ps. I'm using this for a file renaming tool.


UPDATE: Using various ideas below, i've discovered a loop that works


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]

5 Answers 5

1

It's probably easiest to keep dstPath something like "test_%d.jpg", and just pass it a varying count:

dstPath = "test_%d.jpg"
i = 1
while os.path.exists(dstPath % i):
    i += 1
dstPath = dstPath % i # Final name
Sign up to request clarification or add additional context in comments.

1 Comment

a very interesting idea...however i need the flexibility of changing any part of the filename to reflect the original. However, I did change my algorithm above to include a little of both your answer and the answer below. now have dstPathfmt = "%s_%d%s" parts = os.path.splitext(dstPath) fileCounter = 0 #to match the first file "test.jpg" while (os.path.exists(dstPath)): fileCounter += 1 dstPath = parts[0]+"_%d"%fileCount+parts[1]
1

Print out the value of parts[0] each time you go round the loop ... I think you may be surprised,

4 Comments

I think i fixed the problem. I moved parts = os.path.splitext(dstPath) outside of the while loop. works perfectly now.
yes, as per Michael's suggestion. You do understand why your version didn't I hope?
i think it is because as each loop executed, dstPath changed (because the previous loop had modified it). Moving it outside used the dstPath before it changed by the loop.
Yes, but how did it change? First time round part[0] is file-1, you then added -2 to that, so next time round part[0] is file-1-2. You thought that fileCount was screwy, but it was fine.
0

It seems as though your condition os.path.exists(dstPath) is matching the same renamed file multiple times. So for example, it renames test.jpg to test_1.jpg; then renames test_1.jpg to test_1_2.jpg, etc.

Comments

0
for j in range(1,10):
    print("test_{0}.jpg".format(j))

enter image description here

1 Comment

Yes, it renames test.jpg to test_1.jpg; then renames test_1.jpg to test_1_2.jpg, etc. You can see the output of the code by clicking the given link.
0

Update for Python v3.6+ - using formatted string literals:

for n in range(1,51):
    print(f'test_{n}')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.