-3

I am using a Markov chain. When the chain arrives at a particular state, two files (a .png and an .mp3) need to open.

s is the current state of the chain, an integer from 1-59.

I can't seem to find how to open the file with the same number as 's'.

I'm sure it has something to do with %str formatting, but I can't seem to implement it.

img = Image.open('/.../.../s.png')
img.show()

2 Answers2

1

You should use the following line in your code:

img = Image.open('/.../.../{0}.png'.format(s))
Laurent H.
  • 6,316
  • 1
  • 18
  • 40
0

You can format a string using a variable like this

>>> s = 10
>>> '/path/to/file/{}.png'.format(s)
'/path/to/file/10.png'
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118