1

I am very new to Python. I am constructing a string that is nothing but a path to the network location as follows. But it outputs the error: "Python unexpected character after line continuation character". Please help. I saw this post but I am not sure if it applies to my scenario:

syntaxerror: "unexpected character after line continuation character in python" math

s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name
Community
  • 1
  • 1
nikhil
  • 1,578
  • 3
  • 23
  • 52

1 Answers1

3

One of your \ backslashes escapes the " double quote following it. The rest of the string then ends just before the next \ backslash, and that second backslash is seen as a line-continuation character. Because there's another " right after that you get your error:

s_path_publish_folder = r"\\" + s_host + "\" + s_publish_folder "\" + s_release_name
#                                         ^^ not end of string   ||
#                                        ^--- actual string  ---^||
#                                              line continuation /|
#                                                 extra character /   

You need to double those backslashes:

s_path_publish_folder = r"\\" + s_host + "\\" + s_publish_folder "\\" + s_release_name

Better yet, use the os.path module here; for example, you could use os.path.join():

s_path_publish_folder = r"\\" + os.path.join(s_host, s_publish_folder, s_release_name)

or you could use string templating:

s_path_publish_folder = r"\\{}\{}\{}".format(s_host, s_publish_folder, s_release_name)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343