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
7
votes
1 answer

pathlib.py: Instantiating 'PosixPath' on Windows

I cloned thefuck source code from its repository (this is the real project name, yeah, I know...). Tried to install it for development by the following commands: pip install -r requirements.txt python setup.py develop It worked fine. Afterwards…
pjsofts
  • 170
  • 4
  • 18
6
votes
1 answer

How to join list elements to a path with python and pathlib?

Assuming I have a list of unknown length. How would I join all elements of this list to my current path using pathlib? from pathlib import Path Path.joinpath(Path(os.getcwd()).parents[1] , *["preprocessing", "raw data"]) This is not working…
Max2603
  • 403
  • 1
  • 6
  • 23
6
votes
1 answer

Why is pathlib.Path(__file__).parent.parent sensitive to my working directory?

I have a script that's two directories down. ❯ tree . └── foo └── bar └── test.py ❯ cd foo/bar ❯ cat test.py from pathlib import Path print(Path(__file__).parent) print(Path(__file__).parent.parent) When I run it from the…
MatrixManAtYrService
  • 8,023
  • 1
  • 50
  • 61
6
votes
1 answer

get absolute path of file without filename using Python pathlib

I want to replace file_location = os.path.abspath(os.path.dirname(__file__)) with pathlib to get the aboslute path of the file without the filename with using directorypath = pathlib.Path(__file__).resolve() gives me the absolute path + the…
Sator
  • 636
  • 4
  • 13
  • 34
6
votes
1 answer

NotImplementedError: Non-relative patterns are unsupported from attempted looping over all .html files in a directory

I am trying to loop over all html files in a directory but am getting this error: NotImplementedError: Non-relative patterns are unsupported The code I am using is: from bs4 import BeautifulSoup import argparse from pathlib import Path parser =…
Lamma
  • 895
  • 1
  • 12
  • 26
6
votes
3 answers

How to use win environment variable "pathlib" to save files?

I'm trying to use win environment variable like %userprofile%\desktop with pathlib to safe files in different users PC. But I'm not able to make it work, it keep saving in on the running script dir. import pathlib from datetime import datetime a =…
Max
  • 117
  • 2
  • 7
6
votes
4 answers

No module named pathlib2

I've been working on getting Google Assistant working on my Raspberry Pi 3. It is working but I'm having problems getting this specific step to…
Chris Eytcheson
  • 71
  • 1
  • 1
  • 3
6
votes
2 answers

In Python 3.4, what is best/easiest way to compare paths?

Using this code in Python 3.4 and Ubuntu 14.04 do not return True import pathlib path1 = pathlib.Path("/tmp") path2 = pathlib.Path("/tmp/../tmp") print(path1 == path2) # gives False print(path1 is path2) # gives False But normally "/tmp" and…
user3654650
  • 5,283
  • 10
  • 27
  • 28
5
votes
2 answers

How to work with regex in Pathlib correctly?

I want find all images and trying to use pathlib, but my reg expression don't work. where I went wrong? from pathlib import…
Piter
  • 61
  • 1
  • 4
5
votes
1 answer

Avoid the base path in a recursive tree walking

I know how to list recursively all files/folders of d:\temp with various methods, see How to use glob() to find files recursively?. But often I'd like to avoid to have the d:\temp\ prefix in the results, and have relative paths to this base…
Basj
  • 41,386
  • 99
  • 383
  • 673
5
votes
0 answers

Check whether two paths point to the same thing using Pathlib, even when target doesn't exist

How do I check whether to pathlib.Path's point at the same file system location? I hoped I could use .samefile, but that doesn't work when the files/directories don't exist yet. Simple == also doesn't seem to work:…
Bananach
  • 2,016
  • 26
  • 51
5
votes
1 answer

Using pathlib in Google Colab: AttributeError: ‘PosixPath’ object has no attribute ‘ls’

If I run the following code: from pathlib import Path path = Path('data/mnist') path.ls() I get the following error: AttributeError: ‘PosixPath’ object has no attribute ‘ls’ Looking at the Path class in pathlib, I find: def __new__(cls, *args,…
Jacques Thibodeau
  • 859
  • 1
  • 8
  • 21
5
votes
2 answers

Postpending a string to filename (using pathlib Path)

I frequently want to postpend a string to a filename, while preserving the extension. It seems cumbersome to define a function to postpend a string. For example (using pathlib's Path): from pathlib import Path def postpend(filename, string): …
Aturen
  • 93
  • 1
  • 7
5
votes
2 answers

FileNotFoundError when using python -m pytest vs. pytest

I recently changed the IDE I am using to VSCode. For the most part I like it, but there is one particular problem that I can't seem to resolve. I didn't realize it was a problem either, until I moved IDEs. I have a directory structure like…
NewGuy
  • 3,273
  • 7
  • 43
  • 61
5
votes
2 answers

Real path of file string pathlib

I've been searching for what seems like hours for how to get a real path in string format from a pathlib.PosixPath using pathlib. The only solution I can find is this: str(myPathObject.resolve()) This seems messy. Am I missing something or is this…
Preston
  • 7,399
  • 8
  • 54
  • 84