[Python-ideas] os.path.abspath - optional startdir argument
Terry Reedy
tjreedy at udel.edu
Thu Jul 24 23:49:51 CEST 2014
On 7/24/2014 11:30 AM, Wolfgang Maier wrote:
> On 24.07.2014 17:21, Juancarlo Añez wrote:
>> Better yet, use the pathlib module.
Thank for the reminder. I took a better look at it.
> As it stands, the pathlib module is only provisional plus,
'Provisional' means that there *could* be a few api changes that would
break code. The module is not going away.
> IMO, kind of overkill for a simple task like that.
Overkill?
import pathlib as path
import os.path as path
are equally easy
The 'simple task' combines joining, normalizing, and 'absoluting'.
pathlib.Path joins, Path.resolve normalizes and 'absolutes'. Together
they combine the functions of os.path.join, os.path.abspath and
os.path.normpath, with a nicer syntax, and with OS awareness.
>>> path.Path('../../../Python27/lib', 'ast.py').resolve()
WindowsPath('C:/Programs/Python27/Lib/ast.py')
If one starts with a Path object, as would be typical, one can use '/'
to join, as JuanCarlo mentioned.
>>> base = path.Path('.')
>>> (base / '../../../Python27/lib' / 'ast.py').resolve()
WindowsPath('C:/Programs/Python27/Lib/ast.py')
--
Terry Jan Reedy
More information about the Python-ideas
mailing list