[Python-ideas] URLs/URIs + pathlib.Path + literal syntax = ?
Chris Angelico
rosuav at gmail.com
Tue Mar 29 22:51:08 EDT 2016
On Wed, Mar 30, 2016 at 9:17 AM, Michael Selik <mike at selik.org> wrote:
> On Tue, Mar 29, 2016 at 6:06 PM Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>>
>> Koos Zevenhoven wrote:
>> > - Only some URIs (or even URLs) can be reliably distinguished from
>> > file paths. However, those that contain '://' could be automatically
>> > turned into URI objects by p-strings [or Path(...)].
>>
>> No, they couldn't. "hello://world" is a perfectly valid unix
>> pathname (albeit slightly redundant due to the double slash).
>> I would not want Path() deciding that I really meant it to
>> be a URI.
>>
>
> Good point. Even worse:
>
> $ mkdir ftp://www.example.com
> $ tree
> .
> └── ftp:/
> └── www.example.com/
>
> 2 directories, 0 files
How is that "even worse"? It's the exact same thing. (You might need
"mkdir -p" to make this work, as it'll need to create more than one
directory.) You have a directory called "ftp:", and inside that, a
directory called "www.example.com".
rosuav at sikorsky:~/tmp$ mkdir -p ftp://www.example.com
rosuav at sikorsky:~/tmp$ find .
.
./ftp:
./ftp:/www.example.com
rosuav at sikorsky:~/tmp$
But this is a case where the weird edge cases can be dealt with
specially, IMO. There are a *lot* of programs which cannot easily
handle a file name that begins with a hyphen - the solution is to
force a different interpretation, either with a double-hyphen "end of
options", or by using "./-rf" as the file name. The same could be done
here; in the extremely rare situation where you actually want to start
your path with "http:" and then another directory, you have three
options:
1) Path("./http://www.example.com")
2) Path("http:/www.example.com")
3) Path("file://http://www.example.com")
For scripts that need 100% dependable parsing, the third option will
be guaranteed to work. For normal usage, compressing the double slash
to a single one will have the right effect AND canonicalize the name.
This should be safe.
ChrisA
More information about the Python-ideas
mailing list