[Tutor] reading files in Python 3

eryk sun eryksun at gmail.com
Fri Mar 31 02:42:32 EDT 2017


On Thu, Mar 30, 2017 at 8:47 PM, Zachary Ware
<zachary.ware+pytut at gmail.com> wrote:
> In this case, the problem is the bogus Unicode escape that you
> inadvertently included in your path: `\Us...`.  To fix it, either use a
> 'raw' string (`r"C:\Users\..."`) or use forward slashes rather than
> backslashes, which Windows is happy to accept.

Forward slash is acceptable when all you need to support is DOS-style
paths that are limited to DOS semantics and MAX_PATH. A raw string
works, except not for paths ending in backslash and not for unicode
strings in Python 2. A generic solution is to use a pathlib.Path() in
3.x, which normalizes the path to use backslash, or os.path.normpath()
in 2.x.


More information about the Tutor mailing list