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

Path().rglob() hangs script when searching through a large directory

I've noticed that Path().rglob() hangs my script after processing if I pass a large directory (or drive in my case). Is there a way to mitigate that? from pathlib import Path for p in Path('C:\\').rglob('Downloads'): print(str(p)) The above…
malonn
  • 59
  • 1
  • 7
0
votes
1 answer

Can't create file object using pathlib

My code is something like this: from pathlib import Path def make_dir(dirr): if Path(dirr).exists() == False: Path.mkdir(dirr) make_dir('output') 'output' directory doesn't exist. When I'm trying to create it, I get errors,…
ferrum
  • 169
  • 1
  • 13
0
votes
1 answer

Splitting a list into sublists using a for loop

I have a list of files I get from doing a pathlib.glob(). From that list, I need to work with them in chunks depending on their extension (so first .1, then .2, etc.). I can only get the first group of files to show up, and after that I keep getting…
Mauro
  • 93
  • 4
0
votes
0 answers

After each re-execution of the line, a folder is added

I have written a small program code which automatically downloads something from Kaggle. The whole thing should be downloaded to the folder C:/Users/User/Documents/dataset. Here is the problem, when I run the code the first time the path is correct.…
Kazim
  • 175
  • 9
0
votes
1 answer

Mock pathlib.Path.exists

My Python function creates pathlib.Path objects. I want to mock this, so when the code will call pathlib.Path("/tmp/a").exists() it will get True, and when the code will call pathlib.Path("/tmp/b").exists() it will get False. I tried this: import…
0
votes
0 answers

Python fails to execute code line by line and causes folder not found error by reading, writing, copying and deleting folders and csv files

I'm not sure how Python works internally to execute the code but I got a wired issue, which does not always happen but sometimes out of blue. I can even not locate the bug. In the frontend user clicks update button and the corresponding function is…
BabyHai
  • 89
  • 1
  • 9
0
votes
2 answers

Loop over files in different folders

How can I loop over 2 folders? In Apple and all its subfolders, I want to look for Excel files that contain "green". In Banana, I want to look for files that contain "yellow". I explicitly need to specify the folder paths and can't just loop over…
asd
  • 1,245
  • 5
  • 14
0
votes
0 answers

Python: os vs pathlib

What exactly are the advantages of os compared to pathlib ? To me pathlib seems nearly always the better choice, especially because most of the functionality of os can be reproduced by pathlib.os if I am not mistaken. So is the only advantage of os,…
LC117
  • 426
  • 4
  • 12
0
votes
2 answers

iterate through dir of dirs/subdirs printing only subdirs name

I have a base dir with the following structure: name_a/foo/... name_b/bar/... name_c/baz/... This base dir has many subfolders with names_X, and inside each of those, there is a single dir that contains also other files. I´m trying to get a list…
Maxwell's Daemon
  • 587
  • 1
  • 6
  • 21
0
votes
1 answer

Python checking for empty list on a Path subclass is returning 'AttributeError' object has no attribute

Here's my subclass of Path that takes a recursive os.walk(), and maps it to my custom path. Important to note, there's a bug in this code where dirs and files are not being passed correctly. However, my question is: given this bug, and the missing…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0
votes
0 answers

Using pathlib, how can I split a Path object into a list or iterable of individual names, platform agnostic?

Let's say I have a Path object like so: from pathlib import Path path = Path('/Users/Me/Desktop/Media/Pictures') On posix systems, this returns a PosixPath object, and on Windows, a WindowsPath object. Is there a nice, build-in Pythonic way to get…
brandonscript
  • 68,675
  • 32
  • 163
  • 220
0
votes
2 answers

Pathlib read_text as a string literal

I am trying to generate some json data from txt files. The txt files are generated from books, using their ocr, which makes them inestimable (i can't randomly change the chars i don't like, since they could be important) and unreliable (the ocr…
Orsu
  • 405
  • 6
  • 19
0
votes
1 answer

Why doesn't pathlib.Path implement __add__?

Premise A convenient but fragile path might be written in Python as a string fdir = r'mydir/' fname = r'myfile' fpath = fdir + fname This is a terrible practice (np.inf demerits), but the syntax is concise and readable (+1 merits). Question Should…
PetMetz
  • 75
  • 1
  • 7
0
votes
2 answers

How to select single File from os.path.join() result?

I Selected my needed Data Like This: import pathlib from pathlib import Path import glob, os folder = Path('D:/xyz/123/Files') os.chdir(folder) for file in glob.glob("*.json"): JsonFiles = os.path.join(folder, file) print (JsonFiles) As…
zoozle
  • 21
  • 2
0
votes
1 answer

Finding the absolute path to an upstream directory using python pathlib

I have a pathlib.Path object and want to find the absolute path to a parent folder, called "BASE". However, I do not know how much further up the device tree the "BASE" folder is. I just know the pathlib.Path will contain a folder called…
Lukas
  • 27
  • 6