0

i want to create the following string: Plots\0_test.png

The 0 is in variable i. I code this:

path = 'Plots' + '\\'  + str(i) + '_test.png'

and I get the following string: Plots\\0_test.png

I don't know, what is the fault. I hope you can help me.

Christian01
  • 307
  • 1
  • 5
  • 19
  • If you want to be sure it is a valid path, you can use `os` module : `os.path.join('Plots',str(i)+'_test.png')` – Titouan L May 24 '22 at 14:04
  • @TitouanL i'm not sure OP is looking to build a string for a path, otherwise he'd be using the classic slash '/'. – HCKRMVT May 24 '22 at 14:09
  • @HCKRMVT The variable is called `path`. And in order to access a png file it makes sense. But you might be right. – Titouan L May 24 '22 at 14:10
  • Indeed, I made total abstraction of the variable name and file termination... But therefore shouldn't it be the opposite slash ? – HCKRMVT May 24 '22 at 14:13
  • I dont think it matters, you can hardcode a path with `/` characters, but `os.walk()` or `os.path.dirname()` return values with \ (printed as \\ as the question states) – Titouan L May 24 '22 at 14:18
  • 1
    In WIndows, backslash is the standard file separator, but forward slash also works (in Python). In other OSs, forward slash is the file separator. – khelwood May 24 '22 at 14:19

0 Answers0