Re: Obtain the file's path.

2019-09-18 Thread Cameron Simpson

On 18Sep2019 03:36, eryk sun  wrote:

On 9/17/19, Cameron Simpson  wrote:


If you just want this for your running program's internals this may not
matter, but if you're recording the result somewhere then abspath might
get you a more "stable" path in the above scenario.


If a path has ".." components, the abspath() result may be wrong if it
resolves them by removing a parent symlink. The absolute() method of
pathlib.Path does this right by retaining ".." components.

   >>> os.path.abspath('/foo/symlink/../bar')
   '/foo/bar'

   >>> pathlib.Path('/foo/symlink/../bar').absolute()
   PosixPath('/foo/symlink/../bar')

abspath() is also the wrong choice if we're computing the target path
for a relative symlink via relpath(). A relative symlink is evaluated
from the parsed path of its parent directory.


For the record, I agree entirely with Eryk here.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Obtain the file's path.

2019-09-18 Thread Pankaj Jangid
Cameron Simpson  writes:

> On 17Sep2019 15:09, Hongyi Zhao  wrote:
>>See the following two methods for obtaining the file's path:
>>
>>os.path.realpath(file)
>>or
>>os.path.abspath(os.path.expanduser(file))
>>
>>Which is more robust?
>
> My inclination is often to use abspath, because it may better express
> the "intent" of the filename by not "undoing" the effects of symlinks.
>
> So abspath(expanduser("~/media/foo.mp4")) might expand to
> "/home/cameron/media/foo.mp4".
>
> Conversely, realpath(expanduser("~/media/foo.mp4")) might expand to
> "/raid_volume/cameron/media/foo.mp4".
>
> You might ask yourself, why do you need to know the absolute path at
> all? A relative path is usually just fine; it isn't like it won't work
> unless you use it from another directory.
>
Somewhat related to the OP's question.

So what is a good strategy in an application? I am now inclined to use
relative path and working directory both. And when there is change of
runtime context just change the working directory and assemble the
absolute path at runtime.

And to save the working directory use "abspath" as suggested by Cameron.

-- 
Pankaj Jangid
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Obtain the file's path.

2019-09-18 Thread Eryk Sun
On 9/17/19, Cameron Simpson  wrote:
>
> If you just want this for your running program's internals this may not
> matter, but if you're recording the result somewhere then abspath might
> get you a more "stable" path in the above scenario.

If a path has ".." components, the abspath() result may be wrong if it
resolves them by removing a parent symlink. The absolute() method of
pathlib.Path does this right by retaining ".." components.

>>> os.path.abspath('/foo/symlink/../bar')
'/foo/bar'

>>> pathlib.Path('/foo/symlink/../bar').absolute()
PosixPath('/foo/symlink/../bar')

abspath() is also the wrong choice if we're computing the target path
for a relative symlink via relpath(). A relative symlink is evaluated
from the parsed path of its parent directory.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Obtain the file's path.

2019-09-17 Thread Cameron Simpson

On 17Sep2019 15:09, Hongyi Zhao  wrote:

See the following two methods for obtaining the file's path:

os.path.realpath(file)
or
os.path.abspath(os.path.expanduser(file))

Which is more robust?


They're probably equally robust (BTW, you need the expanduser in the 
realpath call as well, if your filename might start with a tilde).


realpath will resolve symlinks and get you a path that does not pass 
through one. abspath is more lexical and just gets you a path which may 
traverse a symlink. Both return a valid absolute path (provided that 
file is an existing file).


My inclination is often to use abspath, because it may better express 
the "intent" of the filename by not "undoing" the effects of symlinks.


Consider the path "~/media/foo.mp4". In my home directory, "media" may 
be a symlink; on at least one machine it is a symlink into our larger 
RAID array.


So abspath(expanduser("~/media/foo.mp4")) might expand to 
"/home/cameron/media/foo.mp4".


Conversely, realpath(expanduser("~/media/foo.mp4")) might expand to 
"/raid_volume/cameron/media/foo.mp4".


If I rearrange things, for example by moving the media directory _and_ 
adjusting the ~/media symlink, then the old result of abspath will still 
work because it traverses the symlink "media". The old result of 
realpath will no longer be correct.


If you just want this for your running program's internals this may not 
matter, but if you're recording the result somewhere then abspath might 
get you a more "stable" path in the above scenario.


You might ask yourself, why do you need to know the absolute path at 
all? A relative path is usually just fine; it isn't like it won't work 
unless you use it from another directory.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Obtain the file's path.

2019-09-17 Thread Hongyi Zhao
See the following two methods for obtaining the file's path:

os.path.realpath(file)
or
os.path.abspath(os.path.expanduser(file))

Which is more robust?
-- 
https://mail.python.org/mailman/listinfo/python-list