File and path in python with pathlib api

common functions and variables

Functions to create or retrieve path objects

Create a path pointing to the current working directory (as returned by os.getcwd()) :
cwd: Path = Path.cwd()

Create a path by concatenating a Path with a String object:
– create a path relative to a foo path:
Example:

cwd: Path = Path.cwd()
print(f'cwd={cwd}')
sub_dir: Path = cwd / '..' / 'foo/'
print(f'sub_dir={sub_dir}')

Output:

cwd=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon
sub_dir=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon\..\foo

Beware: the created path object is in an unresolved state because of ..
As a consequence we cannot apply resolving functions on it such as relative_to()
– create a path child to a foo path:
file_path = foo_path / 'static_resources/file1.txt'

Create a path relative to another path

Here we work with Path objects and not String objects.
The 2 Path objects have to be absolute or relative but not a mix of them.
Example:

base_dir: Path = Path.cwd() / 'fixtures/existing_images'
image_path = base_dir / 'image.jpg'
print(f'base_dir={base_dir}')
print(f'image_path={image_path}')
image_path_relative_to_base_dir = image_path.relative_to(base_dir)
print(f'image_path_relative_to_base_dir={image_path_relative_to_base_dir}')

output:

base_dir=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon\fixtures\existing_images
image_path=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon\fixtures\existing_images\image.jpg
image_path_relative_to_base_dir=image.jpg

Return a string representing the absolute file path of the current executed file:
__file__

create a path for this string:
path = Path(__file__)

Return a string representing the path separator for the current os
os.sep
Example:

cwd: Path = Path.cwd()
print(f'cwd.parts={cwd.parts}')
joined_parts = os.sep.join(cwd.parts)
print(f'joined_parts={joined_parts}')
cwd.parts=('C:\\', 'Users', 'david', 'AppData', 'Roaming', 'talon', 'helper_programs_for_talon')
joined_parts=C:\\Users\david\AppData\Roaming\talon\helper_programs_for_talon

Functions to query path nature

check if a path exist:
path.exists()

check if a path is a file:
path.is_file()

check if a path is a directory:
path.is_dir()

Convert a Path into a String representing a Path and reversely

Path to String:

cwd: Path = Path.cwd()
print(f'cwd={cwd}')
#cwd=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon
path_string: str = str(cwd)
print(f'path_string={path_string}')
#path_string=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon

String to Path :

cwd : Path = Path(path_string)
print(f'cwd={cwd}')
#cwd=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon

Functions to query details of a Path objects

get the logical parent path of a path:
parent_path = foo_path.parent

Return a tuple representing the components in the filesystem path: parts attribute of the Path object.
Example:

cwd: Path = Path.cwd()
print(f'cwd={cwd}')
print(f'cwd.parts={cwd.parts}')

Output:

cwd=C:\Users\david\AppData\Roaming\talon\helper_programs_for_talon
cwd.parts=('C:\\', 'Users', 'david', 'AppData', 'Roaming', 'talon', 'helper_programs_for_talon')

Iterating on files and directories

The glob() function

A single instance method: Path.glob(pattern: str).
It returns a Generator of Paths.
We will see that with just that method we can cover most of cases.
Note:In the examples we will see now, we always use a list comprehension to store the result of iterations.
Of course, it is not an obligation. We do it because it is a common use case and besides it makes examples shorter than with classical loop iteration.

Iterating on files and directories (non recursive)from a specific path

foo_directory: Path = Path(r'D:\foo')
files_and_directories_of_foo: List[Path] = [p for p in foo_directory.glob('*')]

Iterating on files and directories recursively from a specific path

foo_directory: Path = Path(r'D:\foo')
deep_files_and_directories_of_foo: List[Path] = [p for p in foo_directory.glob('**/*')]

Filter Paths

Filter on files :

files: List[Path] = [p for p in foo_directory.glob('**/*') if p.is_file()]

Filter on directories :

directories: List[Path] = [p for p in foo_directory.glob('**/*') if p.is_dir()]

Filter on files with a specific extension (for example .bin extension):

files: List[Path] = [p for p in foo_directory.glob('**/*.bin') if p.is_file()]

Filter on files with specific extensions :

files: List[Path] = [p for p in foo_directory.glob('**/*') if p.is_file()  and p.suffix in {'.bin', '.py'}]

Create path object examples from distinct locations

in the following examples :
– the unit test class is tests/path_examples_test.py
– the existing file which we want to create a path object is static_resources/file1.txt
– sometimes we refer to create the path from another file/function, in this case, the functions called belong to that python file: files/path_examples.py

from pathlib import Path
 
from static_resources import stub as static_resources_stub
 
 
def load_a_file_from_module_path(file: str):
    mod_path = Path(__file__).parent.parent
    print(f'mod_path={mod_path}')
    return mod_path / file
 
 
def load_a_file_from_current_working_directory(file: str):
    return Path.cwd() / file
 
 
def load_a_file_from_specific_path(file: str):
    path = Path(static_resources_stub.__file__).parent / file
    return path

Create a path from current working directory

    def test_create_a_path_from_current_working_directory(self):
        cwd: Path = Path.cwd()
        file_path = cwd / '../static_resources/file1.txt'
        self.assertTrue(file_path.exists())
        self.assertTrue(file_path.is_file())
        # we can do it from another file also
        path = load_a_file_from_current_working_directory('../static_resources/file1.txt')
        self.assertTrue(path.exists())
        self.assertTrue(path.is_file())

Create a path from module path

    def test_create_a_path_from_module_path(self):
        path = Path(__file__).parent.parent / 'static_resources/file1.txt'
        self.assertTrue(path.exists())
        self.assertTrue(path.is_file())
        # we can do it from another file also(not that the parent path  is different according to the location of the file that creates the path)
        path = load_a_file_from_module_path('static_resources/file1.txt')
        self.assertTrue(path.exists())
        self.assertTrue(path.is_file())

Create a path from a specific path

    def test_create_a_path_from_a_specific_path(self):
        path = Path(static_resources_stub.__file__).parent / 'file1.txt'
        self.assertTrue(path.exists())
        self.assertTrue(path.is_file())
        # we can do it from another file also(not that the parent path  is different according to the location of the file that creates the path)
        path = load_a_file_from_specific_path('file1.txt')
        self.assertTrue(path.exists())
        self.assertTrue(path.is_file())
Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *