-1

I am trying to write the following line for a function:

    a.to_csv("~\Desktop\" + file.split('\\')[-1])

but I get

SyntaxError: EOL while scanning string literal

I also tried an f string:

a.to_csv(f"~\Desktop\{file.split('\\')[-1]}")

but I get

SyntaxError: f-string expression part cannot include a backslash

I didn't think this would be such a problem, any help would be appreciated. I am trying to extract the file name part of a path.

ACan
  • 105
  • 1
  • 1
  • 7

1 Answers1

0

Backslashes are used to form special characters like \n and to escape characters that would have an effect, like in your case the quote sign. \" is going to be a literal quote sign in the string and it will not terminate the string. To change that behavior of backslashes, you need to escape them themselves. I.e., if you want a backslash in a string, always type \\ instead of \.

a.to_csv("~\\Desktop\\" + file.split('\\')[-1])

should work.

LukasNeugebauer
  • 1,331
  • 7
  • 10