Comments¶
- It is suggested that you use use
pathlib
package instead ofos.path
.
import os
import inspect
abspath¶
https://docs.python.org/3/library/os.path.html#os.path.abspath
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")
print(inspect.getsource(os.path.abspath))
dirname¶
path.dirname(path.abspath("path.ipynb"))
basename¶
path.basename(path.abspath("path.ipynb"))
path.basename("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))
The parent directory of /
is itself.
os.path.realpath(os.path.join("/", os.path.pardir))
isdir¶
path.isdir("/home/dclong")
isfile¶
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")
import os
# test.dat is a symbolic link to test.txt
os.path.isfile("test.dat")
exists¶
path.exists("/home/dclong")
%ls
dir(path)
expanduser¶
os.path.expanduser("~")
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))
os.path.splitext¶
import os
os.path.splitext("abc.txt")
import os
os.path.splitext("ab")
arr = [0, 1]
o, v = arr
o
v
arr = [[0, 1], [0, 3]]
for o, v in arr:
print(str(o) + ": " + str(v))
os.path.normpath¶
Strips the trailing forward slash from a path.
import os
os.path.normpath("./")
os.path.normpath("/some/path/")
os.path.normpath("/some/path")
os.path.basename("/some/path")
os.path.basename("/some/path/")