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

pathlib Path.rglob fails on long file paths in Windows

I am trying to return a list of all files and subfolders in a particular location. My code is as follows: from pathlib import Path FOLDER_PATH = Path(r'C:\long\file\path\of\138\characters\') I get the error: FileNotFoundError: [WinError 3] The…
teepee
  • 2,620
  • 2
  • 22
  • 47
5
votes
1 answer

Nicest way to find current directory of file in python

I'm looking for a replacement of import os package_dir = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(package_dir,'foo.csv') My working path isn’t the path of the file. So when I want to load a file, I need a way to generate…
subjord
  • 109
  • 1
  • 10
5
votes
0 answers

pyinstaller exe identifiies relative paths but not absolute path

I am using pyinstaller to create EXE from a script. The location of script and exe are as follows: Script: root-dir\subdir1\subdir2\src\scriptabc.py Exe: root-dir\subdir1\subdir2\exe\scriptabc.exe To create Exe I run cmd commands from Exe…
MakJ
  • 119
  • 1
  • 8
5
votes
1 answer

How to iterate through directories in Python using Pathlib

I'm working with Python 3 and I need to perform some operations on folders, using Pathlib and checking if they are folders. The operation I'm gonna do is something like this: from pathlib import Path source_path = Path("path_directory_string") for…
5
votes
0 answers

Python Path.rglob failing on network error when encountering folder without permission

I am new to Python and have been using this site as a reference...thanks for everything, I have learned a ton. First question: I am running a basic recursive file search with Path.rglob(). I am running into a error when it encounters a folder that…
Eric A.
  • 51
  • 3
5
votes
1 answer

Processing non-UTF-8 Posix filenames using Python pathlib?

I'm trying to use the pathlib module that became part of the standard library in Python 3.4+ to find and manipulate file paths. Although it's an improvement over the os.path style functions to be able to treat paths in an object-oriented way, I'm…
Arjen Meek
  • 53
  • 5
4
votes
1 answer

Retrieve latest file with pathlib

I primarily use pathlib over os for paths, however there is one thing I have failed to get working on pathlib. If for example I require the latest created .csv within a directory I would use glob & os import glob import os target_dir =…
DECROMAX
  • 194
  • 3
  • 13
4
votes
0 answers

How can I represent stdin and stderr with pathlib.Path?

I love the pathlib.Path api and use it a lot for quick cli tools. Especially with typer. I have a few tightly related questions: In UNIX cli commands - is the de facto standard for stdin. Is that the same under Windows? Is there a clean,…
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
4
votes
1 answer

Python pathlib match function doesn't work

pathlib match(pattern) is documented as matching a path against a provided glob-style pattern but it doesn't work >>> Path("w/x/y/z").mkdir(parents=True) >>> list(Path().glob("w/**/z")) [PosixPath('w/x/y/z')] >>>…
no step on snek
  • 324
  • 1
  • 15
4
votes
2 answers

Error when analysing code (pylint) on Spyder

I use Spyder 5 (Python 3.7.9), although I also installed Python 3.9 but not with Spyder. When I press F8 to run a Static Analysis Code, a message displays: Traceback (most recent call last): File "D:\obj\windows-release…
Koelite
  • 41
  • 1
4
votes
1 answer

Python pathlib Path().mkdir() applies desired mode to final directory, but mode-umask mode to parents - bug?

I am using pathlib to set up a folder structure, for which I'd like permissions set to drwxrwx--- (770) for all folders in the tree. My current code…
Dave
  • 312
  • 3
  • 11
4
votes
4 answers

KeyError with os.environ[] accessing variable from .env file

I'm trying to build a slackbot and retrieve the slack token from a separate .env file. When I run it, I get thrown an error that looks like this: raise KeyError(key) from None KeyError: 'SLACK_TOKEN' The code for the bot (ShoppingListBot.py) is…
fnaimi
  • 51
  • 1
  • 4
4
votes
1 answer

Is there a pathlib method for finding the complement of one path in another?

Given two Python Pathlib paths in which one is a suffix of the other (of unknown depth), x = Path("/a/b/c/d/e.ext") y = Path("d/e.ext") is there a "nice" way to find the complement of y in x? I.e. find: z = Path("/a/b/c") I'm thinking of perhaps a…
Cabbage soup
  • 1,344
  • 1
  • 18
  • 26
4
votes
1 answer

Pathlib 'normalizes' UNC paths with "$"

On Python3.8, I'm trying to use pathlib to concatenate a string to a UNC path that's on a remote computer's C drive. It's weirdly inconsistent. For example: >>> remote = Path("\\\\remote\\", "C$\\Some\\Path") >>>…
Elad Avron
  • 1,411
  • 1
  • 18
  • 28
4
votes
2 answers

How do I get the child folder name of the path besides these methods?

Of the given path like "level1/level2/level3/", I'd like pass it through some operation and get the result like "level3/". So I made two trials like these: TRIAL 1: After finding parent property within the Path object, I looked for something close…
Kiran Racherla
  • 219
  • 3
  • 12