Search code examples
pythonimagepathbraces

using curly braces in filepaths


Trying to create animation effect by for looping images on a specific file path and found that: THIS WORKS:

img=pygame.image.load(f'/img/{self.char_type}/walk/0.png') 

note 0.png but this DOES NOT:

img=pygame.image.load(f'/img/{self.char_type}/walk/{i}.png')

where i is from the simple FOR loop.

All FAQ posts read so far suggest swapping / for , using f and r, using // or \ and even importing pathlib but all these seem to fail - so anyone got a sure fire method please?


Solution

  • Make a string first and pass it to the loader.

    for i in range(11):
        srt = f'/img/{self.char_type}/walk/{i}.png'
        img=pygame.image.load(srt)