Jason Melo Hall
Jason Melo Hall

Reputation: 662

Numbers Value Changes When Directly After the Backslash os.path.normpath(string)

I am using os.path.normpath and the values change when there are numbers directly after a backslash.

fileString = os.path.normpath("server:\Projects\05 Project Name\Data\20151021\Master.xlsx")
print fileString

Returns: server:\Projects\Project\Data�51021\MASTER_LIST.XLSX

Notice the '\05' disappeared and the '\20' turned into �.

Why is this happening and how can I fix it?

Upvotes: 2

Views: 80

Answers (1)

MattDMo
MattDMo

Reputation: 102892

The easiest way to solve this is to use a raw string literal:

fileString = os.path.normpath(r"server:\Projects\05 Project Name\Data\20151021\Master.xlsx")
#                             ^

The backslash character denotes an escape sequence in regular strings.

The other way around this is to either use forward slashes as path delimiters, or double backslashes:

"server:/Projects/05 Project Name/Data/20151021/Master.xlsx"

or

"server:\\Projects\\05 Project Name\\Data\\20151021\\Master.xlsx"

Upvotes: 3

Related Questions