from pathlib import Path, PureWindowsPath
import itertools
path = Path(".").resolve()
path
No Trailing Slashes¶
A path object always removes the trailing slashes. And Path can be used to manipulate URLs tool, which is convenient.
Path("https://github.com/dclong/dsutil//")
Path.absolute¶
Generally speaking, Path.resolve
is preferred over Path.absolute
.
path.absolute()
Path.anchor¶
path.anchor
Path.as_posix¶
path.as_posix()
Path.as_uri¶
path.as_uri()
Path.chmod(mode)¶
Unlike Path.mkdir
,
mode
is the final mode of the file.
It is not affected by the current umask.
help(path.chmod)
Path.cwd¶
Path.cwd
is a static method to get the current working direcotry.
Path.cwd()
Path.drive¶
path.drive
Path.exists¶
path.exists()
Path.expanduser¶
Path.home()
is preferred to Path('~').expanduser()
.
Path("~").expanduser()
Path("~/archives").expanduser()
Path.glob¶
pathlib.Path.glob
returns a generator (rather than list).
Find all Jupyter/Lab notebooks in the current directory.
path = Path()
path.glob(pattern="*.ipynb")
Find all CSS files in the current directory.
list(path.glob(pattern="*.css"))
Find all Jupyter/Lab notebooks files under the current directory and its sub-directories.
nbs = Path().glob("**/*.ipynb")
len(list(nbs))
Path.home¶
Both Path.home()
(preferred) and Path('~').expanduser()
returns a new Path object representing the user's home directory,
which is the same as os.path.expanduser('~')
.
However,
Path('~').resolve()
does not return a new Path object representing the user's home directory!
Path.home()
Path("~").expanduser()
Path("~").resolve()
Path.iterdir¶
Iterate the content of the directory.
The code below shows the first 5 files/folders under path
.
[p for p in itertools.islice(path.iterdir(), 5)]
Path.mkdir(mode=0o777, parents=False, exist_ok=False)¶
The option
parents=True
creates missing parent directories as needed. The optionexist_ok=True
makes FileExistsError to be omitted.path.mkdir(parents=True, exists_ok=True)
is equivalent to the shell commandmkdir -p path
.By default, the
mode
option has the value777
. However, this doesn't mean that a created directory will have the permission777
by default. The optionmode
works together with umask to decide the permission of the created directory. To make a created directory to have the permission777
, you can set umask to 0 first.:::python import os mask = os.umask(0) Path("/opt/spark/warehouse").mkdir(parents=True, exist_ok=True) os.umask(mask)
Another way is to manually set the permission using the method
Path.chmod
(not affect by the current umask) after creating the directory.
Path.name¶
path.name
Path("/root/abc.txt").name
Path.parent¶
path.parent
Notice that the parent of the root directories (/
, C:
, etc.) are themselves.
path = Path("/")
path
path.parent
path.parent is path
PureWindowsPath("C:").parent
Path.parts¶
path.parts
Path.resolve¶
path.resolve()
Path.relative_to¶
Path("/app/archives/blog").relative_to("/app")
Path("/app/archives/blog").relative_to(Path("/app"))
Path.rename(target)¶
On Windows, if target exists, FileExistsError will be raised. The behavior of Path.rename on Linux is as below (assume the user has permissions):
- If target is an existing file, it is overwritten silently.
- If target is an existing empty directory, it is overwritten silently.
- if target is an existing non-empty directory, an OSError (Errno 39) is thrown.
If you want to overwrite existing target unconditionally,
you can use the method shutil.copytree(src, dst, dirs_exist_ok=True)
and then remove the source directory.
!rm -rf test1 && mkdir -p test1 && touch test1/1.txt && ls test1/
!rm -rf test2 && mkdir -p test2 && touch test2/2.txt && ls test2/
Path("test1").rename("test2")
!ls test2/
Path.stem¶
path.stem
Path("/root/abc.txt").stem
Path.symlink_to¶
Make this path a symbolic link to target.
Under Windows, target_is_directory
must be True
(default False
) if the link’s target is a directory.
Under POSIX, target_is_directory’s value is ignored.
It is suggested that you always set target_is_directory
to be True
(no matter of OS)
if the link's target is a directory.
Notice that a FileExistsError is throw if the current path already exists.
You can first unlink it (using Path.unlink
)
and then create a symbolic link again using Path.symlink_to
.
import tempfile
Path("/tmp/_12345").symlink_to(path, target_is_directory=True)
!ls /tmp/_12345 | head -n 5
Path.__str__
¶
path.__str__()
str(path)
Path.with_name¶
Return a new path with the name changed. If the original path doesn’t have a name, ValueError is raised.
path = Path("/root/abc.txt")
path
path.with_name("ABC.txt")
path.with_name(path.name.replace("abc", "ABC"))
Or another way is to manipulate the underlying string of the path directly.
str(path).replace("abc", "ABC")
Path.with_suffix¶
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead. If the suffix is an empty string, the original suffix is removed.
path = Path("/root/abc.txt")
path
Change the file extension to .pdf
.
path.with_suffix(".pdf")
Remove the file extension.
path.with_suffix("")
Examples of Using pathlib.Path¶
Rename files in the current directory.
for path in Path(".").iterdir():
if path.suffix == ".txt":
path.rename(path.name.replace("1m", "100k"))