pathlib

Richard Damon Richard at Damon-Family.org
Tue Oct 1 06:43:00 EDT 2019


On 10/1/19 1:24 AM, DL Neil via Python-list wrote:
> On 1/10/19 1:09 AM, Chris Angelico wrote:
>> On Mon, Sep 30, 2019 at 9:54 PM Dan Sommers
>> <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>>> I would have said the same thing, but the docs⁰ disagree:  a
>>> PurePath represents the name of (or the path to) a file, but a
>>> Path represents the actual file.
>>>
>>>
>>>https://docs.python.org/3/library/pathlib.html
>>
>> I don't think it represents the actual file. If it did, equality would
>> be defined by samefile, NOT by the file name.
>>
>>>>> from pathlib import Path
>>>>> import os
>>>>> open("file1", "w").close()
>>>>> os.link("file1", "file2")
>>>>> Path("file1") == Path("file2")
>> False
>>>>> Path("file1").samefile(Path("file2"))
>> True
>>>>> Path("file1") == Path("file1")
>> True
>
>
> This example involves a "hard link" in Linux - can't recall if it
> applies under MS-Win...
>
> On a Linux system the 'two' files share the same inode, and thus the
> same 'patch of disk', but one can be deleted without affecting the
> other. Thus, borrowing from the above snippet: 

This is sort of like in Python we have

a = []

b = a

now a and b refer to the same list, On Linux, a file name is NOT a file,
but a reference to it. A file may have many names (or even no names
after you unlink the last name with the file still open),

You can also have a file name (aka a path) that created multiple files
that exist at the same time (create the file and keep it open, unlink or
rename the file, and you can create another with the original name).

We also have relative paths, relative paths specify a file path relative
to something, but don't keep track of that something, and as such may
truly represent multiple real files.

These show that these paths CAN'T actually represent the files,

-- 
Richard Damon




More information about the Python-list mailing list