0

I am trying to append a string value of a variable to end of a string. I am getting a missing closing quote.

My Python snippet is:

from Utilities.HelperMethods import read_from_file

class DataCategoriesPage_TestCase(BaseTestCase):
    def test_00001_add_data_categories(self):
        project_name = read_from_file("project_name")
        administration_page.enter_location_for_export(r"\\STORAGE-1\Testing\Test Data\ClearCore\Exports\5\" + project_name)

What is the correct syntax?

The value of the variable project name is "selenium_regression_project_09/04/2016"

I would like to add this to the end of the string path \\STORAGE-1\Testing\Test Data\ClearCore\Exports\5\

3 Answers 3

4

You can't end a raw string literal with a \, because a backslash can still be used to escape a quote. Quoting the string literal documentation (from the section on raw string literals):

String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).

This looks like a file path, so use os.path.join() to concatenate the file path parts:

import os.path

administration_page.enter_location_for_export(
    os.path.join(
        r"\\STORAGE-1\Testing\Test Data\ClearCore\Exports\5",
        project_name))

Note that the raw string no longer needs to end with a backslash now.

Sign up to request clarification or add additional context in comments.

2 Comments

This is because the backslash is escaping the closing quotes
Thanks for your help. Got it now.
2

Martijn Peters has given the correct answer for your problem (use os.path.join), but there is a solution to the problem of having a literal with lots of back-slashes (so you want to write it as a raw literal), that ends with a back slash. The solution is to write most of it as a raw literal, but write the trailing back-slash as an ordinary (escaped) literal. Python will concatenate two adjacent string literals into one. So in your case, the literal would be written as:

    r"\\STORAGE-1\Testing\Test Data\ClearCore\Exports\5" "\\"

This could conceivably be useful to someone writing some other back-slash heavy code (e.g. regular expressions).

Comments

1

Apparently you can't end a raw string with a backslash - that will escape the final quotation mark. (Bug?). Try:

r"\\STORAGE-1\Testing\Test Data\ClearCore\Exports\5" + "\\" + project_name

1 Comment

It's not a bug, it's a deliberate design decision. It means you can embed a quote in the string by back-slash escaping it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.