Questions tagged [pathlib]

This tag is for questions about the Python built-in library pathlib, which offers classes representing filesystem paths with semantics appropriate for different operating systems.

This module offers classes representing filesystem paths with semantics appropriate for different operating systems. 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.

Its strength lies in that it offers a powerful Object Oriented interface for working with paths, as opposed to the string-based interface offered by os.path.

This table offers a mapping between various os functions to their corresponding PurePath/Path equivalent.

635 questions
10
votes
3 answers

Python 3.6 pathlib Path change name parent directory

The new Path package from the pathlib library, which has been added from Python 3.4, seems a powerful replacement of approaches such as os.path.join(), but I've some trouble working with it. I have a path that can be anything…
NumesSanguis
  • 5,832
  • 6
  • 41
  • 76
10
votes
1 answer

Get a top level from Path object of pathlib

I use pathlib to match all files recursively to filter the files based on their content. Then I would like to find what is the top level of the folder of this file. Assume the following. I have a file in the folder: a/b/c/file.log I do the search…
desa
  • 1,240
  • 12
  • 31
10
votes
2 answers

Getting the target of a symbolic link with pathlib

Is there a way to get the target of a symbolic link using pathlib? I know that this can be done using os.readlink(). I want to create a dictionary composed by links and their target files. links = [link for link in root.rglob('*') if…
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
10
votes
1 answer

Python 3.4+: Extending pathlib.Path

The following code is what I tried first, but some_path.with_suffix('.jpg') obviously returns a pathlib.PosixPath object (I'm on Linux) instead of my version of PosixPath, because I didn't redefine with_suffix. Do I have to copy everything from…
Joschua
  • 5,816
  • 5
  • 33
  • 44
9
votes
2 answers

Trying to use / on Path object Python

The program takes an optional command line argument (which is meant to be a directory path) I am using python pathlib and shutil to move files. Here's the code: from pathlib import Path path = Path(sys.argv[1]) shutil.move(path / file, path /…
user13364040
9
votes
1 answer

Mock/Test Calls to Path.open

I am attempting to write a unit test for a function that calls the open method on a pathlib.Path. I am able to successfully mock the open method without issue, but verifying the function is having the correct behavior is difficult. See the sample…
Adam
  • 181
  • 1
  • 6
9
votes
1 answer

Python rglob pattern for directory search

I try to get the name of subdirectories with Python3 script on Windows10. Thus, I wrote code as follows: from pathlib2 import Path p = "./path/to/target/dir" [str(item) for item in Path(p).rglob(".")] # obtained only subdirectories path names…
tsekine
  • 103
  • 1
  • 1
  • 5
9
votes
0 answers

Getting absolute path without resolving symlinks in python

There are many examples of resolving the path of a file while following symlinks, and I'd like to get the absolute path to a file without resolving symlinks. I think this is the solutions: ex=Path('example.txt') abs_ex =…
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
9
votes
1 answer

How can I convert a pathlib.Path object to a string?

I read that pathlib is the new Python way to deal with paths. So I do: with open(pic_name, 'wb') as image: image.write(download.content) image_path = Path(pic_name).resolve() return image_path When I print image_path I get the full path…
xavier
  • 746
  • 2
  • 12
  • 22
8
votes
1 answer

How can I output paths with forward slashes with pathlib on Windows?

How can I use pathlib to output paths with forward slashes? I often run into programs that accept paths only with forward slashes, but I can't figure out how to make pathlib do that for me. from pathlib import Path, PurePosixPath native =…
idbrii
  • 10,975
  • 5
  • 66
  • 107
8
votes
3 answers

Best way to handle path with pandas

When I have a pd.DataFrame with paths, I end up doing a lot of .map(lambda path: Path(path).{method_name}, or apply(axis=1) e.g: ( pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']}) .assign(full_path=lambda df:…
ClementWalter
  • 4,814
  • 1
  • 32
  • 54
8
votes
2 answers

Is the ordering of pathlib's `glob` method consistent between runs?

Will Path('.').glob('*.ext') produce consistent ordering of results (assuming the files being globbed don't change)? It seems the glob ordering is based on the file system order (at least, for the old glob package). Will pathlib's glob order be…
golmschenk
  • 11,736
  • 20
  • 78
  • 137
8
votes
1 answer

Python pathlib: Resolve full path to symbolic link without following it

Let's say I have a link/home/me/folder/link that points to /home/me/target. When I call pathlib.Path("link").resolve() from /home/me/folder/, it will return the resolved path to the target, not the resolved path of the link. How can I get the…
kuropan
  • 774
  • 7
  • 18
7
votes
2 answers

How to create a pathlib relative path with a dot starting point?

I needed to create a relative path starting with the current directory as a "." dot For example, in windows ".\envs\.some.env" or "./envs/.some.env" elsewhere I wanted to do this using pathlib. A solution was found, but it has a kludgy replace…
bearcat
  • 706
  • 1
  • 7
  • 12
7
votes
3 answers

Python pathlib.Path - how do I get just a platform independent file separator as a string?

I am creating a format string that is different based on class, that is used to generate a filename by a generic class method. I'm using the Python 3.4+ pathlib.Path module for object-oriented file I/O. In building this string, the path separator is…
LightCC
  • 9,804
  • 5
  • 52
  • 92