Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python for File Systems Navigation Absolutely

Husnain Bustam
Husnain Bustam
592 Points

Getting SyntaxError while passing parameters value to the function.

Course: Python for File Systems || Challenge Task 2 of 2 I am getting SyntaxError when I am passing absolute("/home/kenneth/django", "C:\"). This issue is resolved by using "C:\". I am not sure whether I am getting this error due to wrong function parameter value or the function I have written is wrong. Can you please share your thoughts? Thanks.

absolute("/home/kenneth/django", "C:\") SyntaxError: EOL while scanning string literal

absolute.py
import os 
def absolute(path_string, root_string):
    value_path = os.path.isabs(str(path_string))
    value_root = os.path.isabs(str(root_string))
    if value_path == False:
        print (root_string + path_string)
    else:
        print (path_string)

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

It looks like you've inadvertently created an escape sequence. Here's a quick challenge: Put a double-quote (") in the middle of a Python string literal. You might try something like this:

my_string = "Here's a quote: " and this is the rest of the string"

You'll quickly find that you can't. Python thinks the string ends at the quotation marks (") before "and", then there's a bunch of symbols it doesn't recognize, a new string begins, but there's no closing quotation marks (") and the file ends.

The solution to this issue is to use an escape sequence. To tell Python you mean to have those quotation marks (") in the middle of the string and you don't want the string to end, you escape it by putting a backslash (\) before it. Thus, Python will interpret this properly:

my_string = "Here's a quote: \" and this is the rest of the string"
print(my_string) #  Here's a quote: " and this is the rest of the string

Now, let's look at the line where you call absolute:

absolute("/home/kenneth/django", "C:\")

Windows is a little weird in that it uses a backslash (\) to separate path components instead of a forward slash (/) like most other operating systems, but it's significant here because the backslash (\) creates an escape sequence with the quotation marks (") after it, and Python thinks the string never ends

So, how do you escape an accidental escape sequence? Another escape sequence! In this case, you need to escape the backslash (\) and tell Python that you actually want to have a backslash (\) there. You do this just like you would any other character that needs to be escaped: put a backslash (\) in front of it, like this:

absolute("/home/kenneth/django", "C:\\") # Notice the double backslash (\) here

Now Python realizes you actually mean to have a backslash there (but only 1), and will do what you're trying to and pass the string "C:\" as the second parameter