[New-bugs-announce] [issue28611] Syntax error when using raw strings ending with a backslash.

Mallow report at bugs.python.org
Fri Nov 4 12:27:01 EDT 2016


New submission from Mallow:

I'm not sure if this is a bug or not but I've noticed a behavior that seems incorrect.

The use of raw strings, when used for directory paths ending with a back slash (/) creates a syntax error.

How to reproduce
----------------

Code:

print (r"C:\path\to\a\dir\" + "file.ext")

Result: Syntax Error

Why is this an error, (in my perspective)
-----------------------------------------

One could attempt to be storing the directory information in a variable to write to file that is composed later but would be forced to use a cumbersome normal string having to escape all backslashes.

Example:

outputdir = r"C:\path\to\dir\"
filename = r"file.ext"
writetofile(outputdir + filename)

Argument for why the workaround is not a fix
--------------------------------------------

I believe I read somewhere that python is smart enough to deal with filepaths correctly on linux and windows if you were to switch the slashes. So technically 
outputdir = r"C:/path/to/dir/" 
would work
however this is hard on the workflow since I find it easier to copy and paste paths within windows.

I guess it wouldn't be too unreasonable to do something like:
r"C:\path\to\dir/"

----------
files: Capture.PNG
messages: 280057
nosy: princemallow
priority: normal
severity: normal
status: open
title: Syntax error when using raw strings ending with a backslash.
type: compile error
versions: Python 3.5
Added file: http://bugs.python.org/file45352/Capture.PNG

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28611>
_______________________________________


More information about the New-bugs-announce mailing list