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

Python Glob Orignal Directories using Regex

I have a bunch of directories that follow this naming convention: foo foo.v2 foo_v01 foo_v02 foo_v03 bar bar.v3 bar_v01 bar_v02 I am looking for a regex expression to only glob original directories (foo and foo_v01; bar and bar_v01). I'm using the…
cooldood3490
  • 2,418
  • 7
  • 51
  • 66
0
votes
1 answer

skimage.io.imread behaving differently when giving a string or Path

I just stumbled upon a weird situation with skimage.io.imread. I was trying to open a MultiPage TIFF (dimensions: 96x512x512) like this: import argparse from pathlib import Path import numpy as np from skimage import io def…
piponazo
  • 478
  • 2
  • 4
  • 10
0
votes
1 answer

How do I recursively iterate over just the files in a directory using pathlib?

I have a directory, and I want to get all the files (no directories) inside it, and all the files inside of any of its subdirectories and so on.
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
0
votes
2 answers

How to fix ''PosixPath' object has no attribute 'encode'" error using librosa.load?

I'm starting learning basic feature extraction with librosa and was trying reading and storing ten kick drums with pathlib, but it doesn't work since I always getting an encoding error, where as there is no error without pathlib. I tried changing…
mandaleybro
  • 23
  • 2
  • 7
0
votes
2 answers

pathlib mkdir creates a folder by filename

I have the following preexisting folder in my machine D:\scripts\myfolder I want my script to create a folder named logs and create a file log.txt in it. So the path would look like D:\scripts\myfolder\logs\somelog.txt So I used p =…
ontherocks
  • 1,747
  • 5
  • 26
  • 43
0
votes
1 answer

How does pathlib's glob() handle nonexistent and invalid paths?

Say I have a path to a nonexistent directory: dirpath = Path("this/directory/doesnt/exist") or even a completely invalid path: dirpath = Path(r"D:\:$`~[]*/'/..") If I call dirpath.glob('whatever'), one of two things can happen: It can throw an…
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
0
votes
2 answers

What does pathlib.Path.cwd() return?

pathlib.Path.cwd() returns a different value depending on what computer I use (two Windows PCs - one at work, one at home). Project structure (see…
0
votes
1 answer

Path.write_bytes corrupts files transferred with FTP.retrbinarary

I have subclassed ftplib.FTP in order to download some xml files from a FTP server. I have used FTP.retrbinary with Path.write_bytes as callback. Script run successfully but xml files are smaller in size and not readeble by any browser or ET.parse.…
agossino
  • 21
  • 3
0
votes
1 answer

Python Pathlib IndexError

I'm having trouble with using following lines on my AWS instance. Code works perfectly fine on my Macbook. What's the problem here? I've checked directory, it exists, there is not problem with path. Image - code output s =…
buraktokman
  • 35
  • 1
  • 8
0
votes
1 answer

In Python Getting the folder wrong with pathlib ( \u2069 )

I can not make the following code work in my mac and I don't know why. import os from pathlib import Path list_file_name = 'listoffiles.txt' list_of_files_path = Path('Users⁩/jose⁩/CODE⁩/OPS') list_file_with_path= os.path.join…
JFerro
  • 3,203
  • 7
  • 35
  • 88
0
votes
1 answer

Using pathlib, parent's parent just returns the first parent itself (parent.parent == parent)

I'm trying to find the parent directory of the directory that my script is located in: this = pathlib.Path(__name__) parent = this.parent parent2 = parent.parent But printing them out shows that the second .parent isn't working: print(this,…
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
0
votes
1 answer

I'm making a script to generate folders and move images to them in Python on Windows 10 using shutil, but it's throwing the errors below

I'm getting these errors: Traceback (most recent call last): File "file_mover.py", line 41, in shutil.copy(f, os.path.join(path_to_export_two, Path("/image/"))) File "C:\Program Files (x86)\Microsoft Visual…
0
votes
1 answer

Windows! Path String \ gets converted to \\\\

This Code works just fine on Ubuntu and MacOs but gives me headaches on windows. manifest_zip_path = Path(zip_path).name / Path("manifest") tar = tarfile.open(zip_path, "r:gz") f = tar.extractfile(str(manifest_zip_path)) There is a Path object…
ahv2j5dc
  • 11
  • 5
0
votes
1 answer

Incorrect output: Extracting text from pdf's,docx's pptx's will not output in their own spearte line

I created a function that will open each file in a directory and extract the text from each file and output it in an excel sheet using Pandas. The indexing for each file type seems to be working just fine.However the extracted text from each file…
Hugo
  • 49
  • 4
0
votes
1 answer

Python 2.7 path replace string

I need to replace some string by others. I'm using function pathlib to do that it's working fine but I have a problem when there is two same string in my file and I need to change just one My file (wireless.temp) is like this example config 'conf' …