os.path.isfile

Steve D'Aprano steve+python at pearwood.info
Sat Feb 11 23:53:48 EST 2017


On Sun, 12 Feb 2017 03:20 pm, eryk sun wrote:

> On Sun, Feb 12, 2017 at 3:52 AM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>> In Python, you should always use forward slashes for paths, even on
>> Windows.
> 
> There are cases where slash doesn't work (e.g. some command lines;
> \\?\ prefixed paths; registry subkey paths), so it's simpler to follow
> a rule to always convert paths to backslash on Windows, using
> os.path.normpath or pathlib.Path.

Okay, that makes sense. But we agree that when writing paths as string
literals, you should (nearly) always use forward slashes in Python.

Why not raw strings? Because raw strings aren't actually fully raw, and
consequently they don't allow the string to end with a backslash:


py> s = r'documents\'
  File "<stdin>", line 1
    s = r'documents\'
                    ^
SyntaxError: EOL while scanning string literal


(I still don't understand why this isn't just treated as a bug in raw string
parsing and fixed...)



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list