3eternus
3eternus

Reputation: 33

Python object to file path? Error in Syntax

When I trying to add the following file path,

data = ‘c:/vehicles/etc’

I am wondering why I get the following error:

SyntaxError: invalid character in identifier ??

In addition to this, I have also tried:

data = 'C:\vehicles'

which produces a similar error:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Is there something new in python I'm missing to set up file paths?

Upvotes: 1

Views: 377

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405745

data = ‘c:/vehicles/etc’

Here you have fancy quotes that you probably copy/pasted from somewhere, like a Word document. Delete those and type in regular quotes.


data = 'C:\vehicles'

Here you need to escape the \ character so Python knows it's a literal \ and not an escape sequence.

data = 'C:\\vehicles'

Alternatively, prefix the sting literal with an r to tell Python it's a raw string.

data = r'C:\vehicles'

Upvotes: 1

Related Questions