>>> import os
>>> os.getcwd()
/home/you
>>> os.path.abspath('')
/home/you
>>> os.path.abspath('.ssh')
/home/you/.ssh
>>> os.path.abspath('/home/you/.ssh')
/home/you/.ssh
>>> os.path.abspath('.ssh/../foo/')
/home/you/foo
os.getcwd() returns the current working directory.
Calling os.path.abspath with an empty string returns the current working directory, same as
os.getcwd().
Calling os.path.abspath with a partial pathname constructs a fully qualified pathname out of it, based on
the current working directory.
Calling os.path.abspath with a full pathname simply returns it.
os.path.abspath also normalizes the pathname it returns. Note that this example worked even though I
don't actually have a 'foo' directory. os.path.abspath never checks your actual disk; this is all just string
manipulation.
The pathnames and filenames you pass to os.path.abspath do not need to exist.
os.path.abspath not only constructs full path names, it also normalizes them. That means that if you are in the
/usr/ directory, os.path.abspath('bin/../local/bin') will return /usr/local/bin. It
normalizes the path by making it as simple as possible. If you just want to normalize a pathname like this without
turning it into a full pathname, use os.path.normpath instead.
Like the other functions in the os and os.path modules, os.path.abspath is cross−platform.
Comments
Post a Comment
https://gengwg.blogspot.com/