yohhoyの日記

技術的メモをしていきたい日記

raw文字列の落とし穴

Pythonのraw文字列(raw string)記法では、その末尾を奇数個のバックスラッシュ(\)とできない。Windows上のフォルダパス名を表現するときなどで特に注意。

# SyntaxError: EOL while scanning string literal が発生
path_ng = r"C:\User\foobar\"
path_ok = r"C:\User\foobar" + "\\"

パス名の文字列操作であれば、os.pathモジュール日本語)を利用した方がよい。

Python 3.4, The Python Language Referenceより該当箇所引用。

Even in a raw string, 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). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

2. Lexical analysis - 2.4.1. String and Bytes literals

関連URL