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

Python, rename files in directory

I have a folder of xlsx files that I want to edit (add columns...) and save with a new name. This code here works but saves in a new directory with same name. How can I modify the second last line of the code to give it an extension to the original…
LZed
  • 1
  • 2
-1
votes
3 answers

Get all subdirectories that match pattern

I'm using Python 3.7.7. I have this code that get all the subdirectories: from pathlib import Path # Get all subdirectories. p = Path(root_path) dir_lst = [str(x) for x in p.iterdir() if x.is_dir()] But now I need to get all the subdirectories…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
-1
votes
1 answer

How to get list of subdirectories?

I have such structure of folders: some_directory/subdir1/subdir1_1 some_directory/subdir1/subdir1_2 some_directory/subdir2/subdir2_1 etc. My code below returns me : ['subdir1', subdir2] Code: import os from pathlib import Path IMG_IN_PATH =…
-1
votes
1 answer

Tkinter PhotoImage doesn't exist

I was trying to get the movement prototype for my application working when I stumbled across an error, this was with Tkinter's PhotoImage. At first, I decided that I would use Pathlib but I am starting to believe that is a mistake (I do still need a…
NaNdy
  • 99
  • 8
-1
votes
1 answer

How is this loop breaking openpyxl

The program is supposed to scrape a bunch of text file with a regex pattern and through the results into an excel file. From what I can tell, if I uncomment the for statement specifically, it'll go ahead and make the xlsx file. If I uncomment…
-1
votes
1 answer

Having trouble converting paths in Pathlib

I have to take in a file path that looks like this: 'C:/Users/xxx/Desktop/test_folder' It gets stored into a variable as a string so: path_intake = 'C:/Users/xxx/Desktop/test_folder' I want to assign that path to my p = Path(path_intake) But,…
Hugo
  • 49
  • 4
-2
votes
1 answer

How to get a list of all parents in directory that their children are files in python?

I have a tree of directories that in the end of the each directory will always be 2 files, one as suffix ".txt" and one will always be ".jpg", for example: "rootFolder/a/a.txt", "rootFolder/a/b.jpg" "rootFolder/b/c/a.txt", "rootFolder/b/c/b.jpg" and…
Dump Eldor
  • 92
  • 1
  • 11
-2
votes
1 answer

How to use function output as a input to other function in the same script?

I want to use preprocessed image as an input to def infer_text that will return me annotations. SO, how can I do this, what should I pass to infer_text function? image_folder = Path("/home/Tasks/NM_spanish/Invoices_pdf") def…
aarya
  • 83
  • 1
  • 8
-2
votes
1 answer

Path.replace(target) does not work with my approach

Pathlib is suggested instead of os.module because is newer. However I find less documentation about Pathlib if I look for. I show an issue that give me problems. Initial condition In a directory I have the following files: I want to change the…
simone100
  • 43
  • 1
  • 6
-2
votes
2 answers

python generator parsing one file at a time

I often have a folder with a bunch of csv files or excel or html etc. I get tired of always writing a loop iterating over the files in a folder and then opening them with the appropriate library, so I was hoping I could build a generator that would…
-2
votes
2 answers

How to handle windows path in Python?

The requirement of code is I have to take the file path from the user in the console and perform some action on that file. Users can give paths in windows style or mac style. for the mac or Linux, the path code is working fine but for the windows…
-2
votes
1 answer

Python code style: spaces around joinpath operator of pathlib

This bothers me for quite some time. Do you put spaces around join path operator / of pathlib library? Consider the following example: root_dir = Path('root') sub_dir = root_dir / 'folder1' / 'folder2' Since the join operator is still an operator,…
Serhiy
  • 4,357
  • 5
  • 37
  • 53
-2
votes
1 answer

How does PathLib Works

I was using Pathlib on Python3.x and I found a piece of code that got me curious. from pathlib import Path BASE = Path('/mydir').resolve(strict=True).parent.parent print( BASE / 'Sub-dir') And that works perfectly, printing out: /mydir/Sub-dir I…
-2
votes
1 answer

Unzip all the items from the output of rglob() method using pathlib module

I have a folder that contains zip files in subfolders. I want to unzip all the files using this python code. code shows no error but the files are not extracted can't figure out the problem. Thanks in Advance. from zipfile import ZipFile from…
brownbunny
  • 55
  • 5
-2
votes
1 answer

error in pathlib module in python when Iam using vscode

I'd add path form pathlib to my vscode but whenever I call it's modules like path.stat I have an error,and vscode won't allow me to use () for calling from pathlib import Path path=Path("enum/__init__.py") pri=path.stat print(pri) the error…
Arezoo
  • 1
  • 1
1 2 3
42
43