본문 바로가기
Python

[Python] pathlib module (Object-Oriented Filesystem Paths)

by llHoYall 2020. 10. 13.

Python version 3.4 and above basically includes this module.

This module offers classes representing filesystem paths with semantics appropriate for different operating systems.

This module treats the file system path as an object, not just a string.

 

Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

Import the Module

from pathlib import Path

Useful Operators

  • / : The slash operator helps create child paths, similarly to os.path.join().

Useful Properties

  • drive : A string representing the drive letter or name if any.
  • parents : An immutable sequence providing access to the logical ancestors of the path.
  • parent : The logical parent of the path.
  • name : A string representing the final path component, excluding the drive and root, if any.
  • stem : The final path component, without its suffix.
  • suffixes : A list of the path's file extensions.
  • suffix : The file extension of the final component, if any.

Useful Functions

  • is_dir() : Return True if the path points to a directory (or a symbolic link pointing to a directory), False if it points to another kind of file. False is also returned if the path doesn't exist or is a broken symlink; other errors are propagated.
  • is_file() : Return True if the path points to a regular file (or a symbolic link pointing to a regular file), False if it points to another kind of file. False is also returned if the path doesn't exist or is a broken symlink; other errors are propagated.
  • exists() : Whether the path points to an existing file or directory.
  • glob() : Glob the given relative pattern in the directory represented by this path, yielding all matching files.
  • rglob() : This is like calling Path.glob() with '**/' added in front of the given relative pattern.
  • resolve() : Make the path absolute, resolving any symlinks.
  • joinpath() : Calling this method is equivalent to combining the path with each of the other arguments in turn.
  • touch() : Create a file on this given path.
  • mkdir() : Create a new directory on this given path.
  • rmdir() : Remove this directory.
  • rename() : Rename this file or directory to the given target, and return a new Path instance pointing to the target.
  • replace() : Rename this file or directory to the given target, and return a new Path instance pointing to the target.
  • iterdir() : When the path points to a directory, yield path objects of the directory contents.
  • cwd() : Return a new path object representing the current directory.
  • home() : Return a new path object representing the user's home directory.
  • open() : Open the file pointed to by the path.
  • chmod() : Change the file mode and permissions.

Usage

Listing Subdirectories

for x in Path('.').iterdir():
    if x.is_dir():
        print(x)

Listing Python Source Files in Current directory

print(list(Path('.').glob('**/*.py')))

Get Parents

path = Path('.').cwd()

print(path)
# /Users/hoya/Test

print(path.parents[0])
# /Users/hoya

print(path.parents[1])
# /Users

print(path.parents[2])
# /

Resolving Paths

path = Path('.')
print(path)
# .

path = Path('.').resolve()
print(path)
# /Users/hoya/Test

'Python' 카테고리의 다른 글

[Python] Dictionary Data Structure  (0) 2020.12.06
[Python] Templates Static Files in Flask  (0) 2020.10.23
[Python] Getting Started with Flask  (0) 2020.10.11
[Python] Beautiful Soup4 module  (0) 2020.10.09
[Python] requests module  (0) 2020.10.08

댓글