Adding a directory to sys.path with pathlib

From the docs: A program is free to modify this list for its own purposes. Only strings should be added to sys.path; all other data types are ignored during import. Add the path as a string to sys.path: PROJECT_DIR = Path(__file__).parents[2] sys.path.append( str(PROJECT_DIR / ‘apps’) ) PROJECT_DIR is an instance of PosixPath which has all … Read more

Clean way to get the “true” stem of a Path object?

You could just .split it: >>> Path(‘logs/date.log.txt’).stem.split(‘.’)[0] ‘date’ os.path works just as well: >>> os.path.basename(‘logs/date.log.txt’).split(‘.’)[0] ‘date’ It passes all of the tests: In [11]: all(Path(k).stem.split(‘.’)[0] == v for k, v in { ….: ‘a’: ‘a’, ….: ‘a.txt’: ‘a’, ….: ‘archive.tar.gz’: ‘archive’, ….: ‘directory/file’: ‘file’, ….: ‘d.x.y.z/f.a.b.c’: ‘f’, ….: ‘logs/date.log.txt’: ‘date’ ….: }.items()) Out[11]: True

Why does pathlib have both PurePath & Path?

First paragraph in pathlib documentation states: 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. Pure path objects provide path-handling operations which don’t actually access a filesystem. Concrete paths are subclasses of the pure path classes. In … Read more

What is the correct way in python to annotate a path with type hints? [duplicate]

I assume that typical path objects are either Paths or strs, as such you could use a Union. In addition, the more inclusive os.PathLike is preferred over pathlib.Path. Python 3.10 or newer: import os def read_json(path: str | os.PathLike): … Python 3.5 – 3.9: import os import typing def read_json(path: typing.Union[str, os.PathLike]): …

How do I append a string to a Path?

The correct operator to extend a pathlib object is / from pathlib import Path Desktop = Path(‘Desktop’) # print(Desktop) WindowsPath(‘Desktop’) # extend the path to include subdir SubDeskTop = Desktop / “subdir” # print(SubDeskTop) WindowsPath(‘Desktop/subdir’) # passing an absolute path has different behavior SubDeskTop = Path(‘Desktop’) / ‘/subdir’ # print(SubDeskTop) WindowsPath(‘/subdir’) When several absolute paths … Read more

Use pathlib for S3 paths

Using s3path package The s3path package makes working with S3 paths a little less painful. It is installable from PyPI or conda-forge. Use the S3Path class for actual objects in S3 and otherwise use PureS3Path which shouldn’t actually access S3. Although the previous answer by metaperture did mention this package, it didn’t include the URI … Read more

How to make a new Path object from parts of a current path with pathlib?

In order to sum up the comments, the statements are the following: In order to change the file name In [1]: import pathlib In [2]: path = pathlib.Path(“/home/user/to/some/folder/toto.out”) In [3]: path.parent / “other_file.dat” Out[3]: PosixPath(‘/home/user/to/some/folder/other_file.dat’) In order to change one part in the path In [4]: parts = list(path.parts) In [5]: parts[4] = “other” In … Read more

Pathlib vs. os.path.join in Python

pathlib is the more modern way since Python 3.4. The documentation for pathlib says that “For low-level path manipulation on strings, you can also use the os.path module.” It doesn’t make much difference for joining paths, but other path commands are more convenient with pathlib compared to os.path. For example, to get the “stem” (filename … Read more

tech