On Fri, Feb 11, 2022 at 10:26:03AM +0200, Serhiy Storchaka wrote:

> expandvars() does not operate on paths, it operates on strings and
> bytestrings. There is nothing path-specific here.

Paths can be strings and byte-strings too.

When I using my OS shell, and I type:

    ls -l ~/code/

I am passing a path to ls as argument.

And when I do either of these in Python:

    os.listdir(os.path.expanduser('~/code/'))

    os.listdir(os.path.expandvars('${HOME}/code/'))

I am still passing a path to listdir. Its just not a pathlib.Path 
instance, but it is still a path, just as "123 Main Road" is a street 
address and "Jane Doe" is a name.

Of course environment variables are not restricted to being path 
components, but neither are Paths:

    >>> snake = Path('Chordata/Reptilia/Squamata/Serpentes')
    >>> python = snake/'Pythonidae/Python'
    >>> rock_python = python/'sebae'
    >>> burmese_python = python/'bivittatus'

Is this an abuse of Path? Hell yes. What are we, the Path Police? :-)

The point is, regardless of whatever odd non-path-like things people 
might happen to have in environment variables, they also have paths in 
environment variables, and it is a strange lack that pathlib doesn't 
make it easy to construct paths from environment variables.

If people want to shoot themselves in the foot:

    myfile = Path('${HOME}/${LS_COLORS}/file.txt').expandvars()

that's up to them, and who knows, maybe somebody does actually have a 
file with that path, no matter how odd it looks.


> Note that there are two implementations of expandvars(): in posixpath
> and ntpath. You may want to apply Posix substitution on Windows and
> Windows substitution on Linux or macOS, so it cannot be tied to
> PosixPath or WindowsPath.

That's odd. I would expect PosixPath.expandvars to use Posix-style 
substitution, and WindowsPath.expandvars to use Windows-style 
substitution.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/L4V7CC3H7XQFSGQWWZSK4H3MSQBGOPCA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to