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

Getting string representation of pathlib object

I'm writing a python script to merge multiple PDFs into one to put into an output folder. Each PDF will be in a different folder with an employee name on it and it will need to be combined with the corresponding pdf in another folder. Not all…
Jarl2.0
  • 205
  • 1
  • 3
  • 8
15
votes
3 answers

Can't import pathlib

Whenever I write: import pathlib or from pathlib import path I got this: ImportError: No module named pathlib I tried also naming it os.path. I'm on Python 2.7.14 Is pathlib in Python 2 or only in 3? If it's not available, what else can I use?
Nonox
  • 167
  • 1
  • 1
  • 4
15
votes
4 answers

pathlib Path and py.test LocalPath

I have started using pathlib.Path some time ago and I like using it. Now that I have gotten used to it, I have gotten sloppy and forget to cast arguments to str. This often happens when using tox + py.test with temporary directories based on tmpdir…
Alois
  • 197
  • 2
  • 9
15
votes
5 answers

Iterate through files

I'm trying to adapt someone's code for my (Windows 7) purposes. His is unfortunately UNIX specific. He does dir_ = pathlib.PosixPath(str(somePathVariable)) os.chdir(str(dir_)) for pth in dir_: # some operations here Running this, I got…
FooBar
  • 15,724
  • 19
  • 82
  • 171
14
votes
7 answers

I can't load my model because I can't put a PosixPath

I'm setting up a script and I need to use some functions from fast-ai package. The fact is that I'm on Windows and when I define my paths, the function from fast-ai named load_learner can't load the model. I've tried to change the function into the…
Bando
  • 1,223
  • 1
  • 12
  • 31
14
votes
4 answers

Accessing networked file locations with pathlib

I'm trying to test a program using Python's pathlib module. With the os module, you used to be able to access networked drives by just following the same url-like form. But for some reason, you can't do this with pathlib. Or at least I can't figure…
Alex
  • 193
  • 1
  • 7
14
votes
2 answers

Convert WindowsPath to PosixPath

I am using pathlib to manage my paths in my Python project using the Path class. When I am using Linux, everything works fine. But on Windows, I have a little issue. At some point in my code, I have to write a JavaScript file which lists the…
graille
  • 1,131
  • 2
  • 14
  • 33
14
votes
1 answer

Can Python3's pathlib be used portably between Linux and Windows systems?

I'm writing some software in Python3 intended to take a backup of a directory structure on a Windows client, and send it to a Linux server. The problem I'm having is how to deal with Windows and Linux file paths. I need the Windows client to create…
John
  • 2,551
  • 3
  • 30
  • 55
13
votes
5 answers

How to Naturally Sort Pathlib objects in Python?

I am trying to create a sorted list of files in the ./pages directory. This is what I have so far: import numpy as np from PIL import Image import glob from pathlib import Path # sorted( l, key=lambda a: int(a.split("-")[1]) ) image_list =…
PrasadHeeramani
  • 251
  • 1
  • 2
  • 10
13
votes
1 answer

Difference between slash operator and comma separator in pathlib Path

I read that in pathlib we can create a child path by using / between two paths, where a comma would also work. However, I can't find out if there is a difference between the two cases. In the following example, the output is the same: from pathlib…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
12
votes
2 answers

How to check folder / file permissions with Pathlib

Is there a Pathlib equivalent of os.access()? Without Pathlib the code would look like this: import os os.access('my_folder', os.R_OK) # check if script has read access to folder However, in my code I'm dealing with Pathlib paths, so I would need…
NumesSanguis
  • 5,832
  • 6
  • 41
  • 76
12
votes
1 answer

How to use pathlib.Path.expanduser() and amend and use a PosixPath value?

Below shows how I obtained user1's home directory, create a new sub-directory name and create a new sub-directory there via python 3.6's os module. >>> import os.path >>> import os >>> a = os.path.expanduser('~') >>> a '/home/user1' >>> a_sub_dir…
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
12
votes
5 answers

How to normalize a relative path using pathlib

I'm trying to use relative paths in Python, and I want to put my csv files in a separate folder from my python code. My python program is in the following folder: G:\projects\code I want to read this file which is one level…
Dread
  • 789
  • 3
  • 16
  • 31
10
votes
6 answers

skip hidden files and directories in pathlib glob

With from pathlib import Path path = Path("/my/path") for p in path.rglob("*"): print(p) I can get all files and directories in /my/path. How can I skip hidden files and directories? From file .hidden_file dir / file …
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
10
votes
2 answers

How to get the relative path between two absolute paths in Python using pathlib?

In Python 3, I defined two paths using pathlib, say: from pathlib import Path origin = Path('middle-earth/gondor/minas-tirith/castle').resolve() destination = Path('middle-earth/gondor/osgiliath/tower').resolve() How can I get the relative path…
ruancomelli
  • 610
  • 8
  • 17