-2

it 's possible to put a variable into the path in python/linux

for example :

>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image'counter'.jpeg', 'rb'))

I have syntax error when i do that.

betontalpfa
  • 3,454
  • 1
  • 33
  • 65
  • The [documentation](https://docs.python.org/3/library/string.html#format-string-syntax) might be helpful here – DavidG Nov 12 '18 at 09:59
  • @Castelo If an answer here has helped you, standard practice on SO is to accept it. Please accept which ever answer helped you the most. – amitchone Nov 12 '18 at 10:59

3 Answers3

2

You could use an f-string if you’re working in python 3.6+

This is the most efficient method.

counter = 0
filepath = f"/home/user/image{counter}.jpeg"
image = ClImage(file_obj=open(filepath, 'rb'))

Otherwise the second best would be using the .format() function:

counter = 0
filepath = "/home/user/image{0}.jpeg".format(counter)
image = ClImage(file_obj=open(filepath, 'rb'))
Jab
  • 26,853
  • 21
  • 75
  • 114
1

You can use Python's .format() method:

counter = 0
filepath = '/home/user/image{0}.jpeg'.format(counter)
image = ClImage(file_obj=open(filepath, 'rb'))
amitchone
  • 1,630
  • 3
  • 21
  • 45
1

You need string concatenation.

>>>counter = 0;

>>>image = ClImage(file_obj=open('/home/user/image' + str(counter) + '.jpeg', 'rb'))
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
betontalpfa
  • 3,454
  • 1
  • 33
  • 65