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
0 answers

using negative indices with Pathlib Parents method

I'm trying to use groupby on a pandas data structure containing pathlib information and file sizes from a particular drive. I want to total up the storage used at a particular depth of the file directory structure to see which directories are the…
0
votes
1 answer

pathlib2 not working for finding files recursively

I'm using python2.7 and trying to find files recursively in subdirectories but pathlib2 is returning NULL >>> from pathlib2 import Path >>> list(Path('~/text-segmentation/data/choi/').glob('**/*.ref')) [] Whereas if the same thing is done in…
0
votes
3 answers

Python - Append string to existing lines of a file

I have an origin file (fo.log) like below: title1 title2 title3 o11 o12 o13 o21 o22 o23 o31 o32 o33 And a destination file (fd.log) like below: d11 d12 d21 d22 d31 d32 Both files have the same number of lines (that…
Carlos Ost
  • 492
  • 7
  • 22
0
votes
1 answer

Platform Independent File Path for numpy.genfromtxt

I use numpy's genfromtxt function for work all the time. Tomorrow I have to make a presentation on how to use Python (something I've only just started learning 6 weeks ago). The problem is that a lot of my audience will be using Mac's or Linux…
B. Moore
  • 109
  • 1
  • 11
0
votes
1 answer

Quickly check for subdirectories in list

I have two sets of paths, with maybe 5000 files in the first set and 10000 files in the second. The first set is contained in the second set. I need to check if any of the entries in the second set is a child of any entry in the first set (i.e. if…
Rasmus
  • 69
  • 7
0
votes
0 answers

How to test working with directories using pathlib

I have a function that searches for .json files in directory recursively. It uses pathlib. def search(where: Path) -> List[Path]: ... I want to unit test it, so I need to a way create fake Path object with children, so that fake_path.iterdir() and…
Alexey
  • 3
  • 1
  • 3
0
votes
1 answer

Erroneous Output for Path Check in Python 3.6

I was just experimenting with the pathlib library in Python 3.6. I wanted to check if such a file with the given file name exists in the path that I have given. Here is my code: from pathlib import Path f =…
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
0
votes
1 answer

Renaming set of files when old names conflict with new names in Python3

Suppose I have a set of file rename operations: renames={(current_1,new_1),(current_2,new_2),...} It is guaranteed that the files current_1,current_2,... exist and that the new files new_1,new_2,... are unique. All files are instances of…
Saúl Pilatowsky-Cameo
  • 1,224
  • 1
  • 15
  • 20
0
votes
0 answers

Python web scraping stopped working - Invalid File Error

I have been working on a web scraping program and it has recently just stopped working and is giving me the following error. Traceback (most recent call last): File "C:/Users/Bob/Desktop/test 3.5.py", line 7, in with open(saveDB,'rb')…
MrClean
  • 1,300
  • 2
  • 12
  • 17
0
votes
1 answer

No error when stepping through in debug mode but AttributeError otherwise

Example code from pathlib import Path for f in Path().iterdir(): print(f._str) I'm using it to pass to a function but not even this works when running normally or debug without a breakpoint. Using a breakpoint and stepping through it…
Nimitz14
  • 2,138
  • 5
  • 23
  • 39
-1
votes
1 answer

Difference between pathlib.Path().resolve() and pathlib.Path().parent

I'm having a bit of a hard time understanding the difference between pathlib.Path().resolve() and pathlib.Path().parent. If I want to be able to get the directory of the current script, do these serve the same purpose? And are both of these…
CharlieCCC
  • 21
  • 5
-1
votes
1 answer

How to create a directory inside the C:\ProgramData in Python?

How to create a directory inside the C:\ProgramData in Python? I want to create Foo directory inside C:\ProgramData in Python import os from pathlib import Path, PurePath, PosixPath,PurePosixPath,PureWindowsPath,WindowsPath path =…
-1
votes
1 answer

Pathlib alternative for os.walk(dir_name)

Original code : for dir_name, dir_path, file_name in os.walk(dir): doSomething() What can we use from pathlib module instead of os.walk(). Thankss....
prachi sutane
  • 13
  • 1
  • 8
-1
votes
1 answer

Trying to change function parameter from inside using a while loop

This function checks for files in a directory, then if there is already a file with that name it appends a auto increment number such as (1),(2),etc. For some reason my self.count, self.index or self.file_name is not being update after it goes…
-1
votes
1 answer

pathlib File directory (not working) but without the file name (Parent returns two directories up)

If Im in: C:\dir\pythonfile.py when I run Path.cwd() I worry about terminal runs from different places, so (maybe wrong) I rely on Path(file) that returns the right path with the file. If I use ".parent" I get: dir = Path(file).parent # returns C:\…