Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Comments¶
It is suggested that you use use
pathlibpackage instead ofos.path.
import os
import inspectabspath¶
https://
Return a normalized absolutized version of the pathname path.
Notice that a path that is a symbolic link is not automatically traced to the original path.
You have to use the function os.path.realpath
to get the original path of a file.
Notice that os.path.realpath calls os.path.abspath internally
so that you do not need to call os.path.abspath by yourself
when you use os.path.realpath.
os.path.abspath("path.ipynb")'/workdir/learning/0-programming/1-python/3-file_system/os/path.ipynb'print(inspect.getsource(os.path.abspath))def abspath(path):
"""Return an absolute path."""
path = os.fspath(path)
if not isabs(path):
if isinstance(path, bytes):
cwd = os.getcwdb()
else:
cwd = os.getcwd()
path = join(cwd, path)
return normpath(path)
dirname¶
path.dirname(path.abspath("path.ipynb"))'/jupyter/learning/python/file_system'basename¶
path.basename(path.abspath("path.ipynb"))'path.ipynb'path.basename("path.ipynb")'path.ipynb'os.path.pardir¶
os.path.pardir is not a directory but instead a str object '..'.
os.path.realpath(os.path.join("/home/dclong", os.path.pardir))'/home'The parent directory of / is itself.
os.path.realpath(os.path.join("/", os.path.pardir))'/'isdir¶
path.isdir("/home/dclong")Trueisfile¶
Returns True on
1. file
2. symbolic link to file
Returns False on
1. directory
2. symbolic link to directory
import os
# /home/chud is a directory
os.path.isfile("/home/dclong")Falseimport os
# test.dat is a symbolic link to test.txt
os.path.isfile("test.dat")Trueexists¶
path.exists("/home/dclong")True%lspath.ipynb*
dir(path)['__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_get_sep',
'_joinrealpath',
'_varprog',
'_varprogb',
'abspath',
'altsep',
'basename',
'commonpath',
'commonprefix',
'curdir',
'defpath',
'devnull',
'dirname',
'exists',
'expanduser',
'expandvars',
'extsep',
'genericpath',
'getatime',
'getctime',
'getmtime',
'getsize',
'isabs',
'isdir',
'isfile',
'islink',
'ismount',
'join',
'lexists',
'normcase',
'normpath',
'os',
'pardir',
'pathsep',
'realpath',
'relpath',
'samefile',
'sameopenfile',
'samestat',
'sep',
'split',
'splitdrive',
'splitext',
'stat',
'supports_unicode_filenames',
'sys']expanduser¶
os.path.expanduser("~")'/Users/dclong'os.path.realpath(path)¶
os.path.realpath returns the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
Notice that os.path.realpath calls os.path.abspath
so that you do not have to call os.path.abspath by yourself
when you call os.path.realpath.
print(inspect.getsource(os.path.realpath))def realpath(filename):
"""Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path."""
filename = os.fspath(filename)
path, ok = _joinrealpath(filename[:0], filename, {})
return abspath(path)
os.path.splitext¶
import os
os.path.splitext("abc.txt")('abc', '.txt')import os
os.path.splitext("ab")('ab', '')arr = [0, 1]o, v = arro0v1arr = [[0, 1], [0, 3]]for o, v in arr:
print(str(o) + ": " + str(v))0: 1
0: 3
os.path.normpath¶
Strips the trailing forward slash from a path.
import os
os.path.normpath("./")'.'os.path.normpath("/some/path/")'/some/path'os.path.normpath("/some/path")'/some/path'os.path.basename("/some/path")'path'os.path.basename("/some/path/")''