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

How to rename all files to include the directory name?

I'm trying to use a For loop in the code below to go through a list of files and rename them with the file directory's name. import re # add this to your other imports import os for files in os.walk("."): for f_new in files: …
AA2018-2020
  • 341
  • 1
  • 6
  • 16
0
votes
2 answers

pathlib full list of methods

What is the iPython code/command to list the methods in this case from the pathlib module? Rationale I use pathlib for example to switch between genetic data file formats. In this example from myfile.txt to myfile.md (markdown), fileIn = Path.home()…
M__
  • 614
  • 2
  • 10
  • 25
0
votes
1 answer

How to iterate through files using pathlib.glob() when files names have digits of different length

My Directory looks like this: P1_SAMPLE.csv P2_SAMPLE.csv P3_SAMPLE.csv P11_SAMPLE.csv P12_SAMPLE.csv P13_SAMPLE.csv My code looks like this: from pathlib import Path file_path = r'C:\Users\HP\Desktop\My Directory' for fle in…
Leockl
  • 1,906
  • 5
  • 18
  • 51
0
votes
1 answer

I am using Pathlib to open a folder so that i can create a txt file inside of that folder but instead i am getting an error

There is a typeError that I can't seem to fix, it happens after running it and going through the quiz creator. It says the error was from line 61 of the code Exception in Tkinter callback Traceback (most recent call last): File…
0
votes
1 answer

How to find full path of .exe file in Python?

I have to find full path of some .exe files in python. I've tried the following but this always outputs C:\Users\Admin\. os.path.abspath("file.exe")
0
votes
0 answers

importing from directory which imports from another file and loads a file [python]

My dummy project tree is as below: base| |test.py |model |code1.py |code2.py |hi.npy hi.npy is a simpe string stored / any file that I need to load in code1. code2.py import numpy as np def pr(a): print(f'hi…
Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60
0
votes
1 answer

After using len() function to the Path, the Python snippets stops executing the for loop

The snippets does not execute the for loop after the file_counts = len(list(images)). If I drop this line, the loop is working fine. I wonder what happened. Is there anyone who can help me to explain and fix it? from pathlib import Path …
passion
  • 387
  • 4
  • 16
0
votes
0 answers

Writing a list of python console output to text file

I am a newb to python and working on writing a file from the list of devices printed to the python console. I am using pathlib and trying to write the entire list to a text file. I have tried many configurations but for some reason it only writes…
jjevans
  • 1
  • 1
0
votes
3 answers

Replacing Punctuation in List of Files

I am trying to replace a '. ' with a '_' for all files in a directory. The files look like this: 000. utm_homescreen.bak 000. utm_homescreen.yxwz 001. utm_chain_screen.bak 001. utm_chain_screen.yxwz ... Right now I am trying to apply a split and…
James D.
  • 165
  • 4
  • 14
0
votes
1 answer

Creating Directories and Files - Not working as expected (pathlib, .mkdir, .touch)

I'm trying to create some directories and files from a list in Python. The expectation I had of my for loop is that it would check to see if the path exists in the list, determine if it is a file path or not. If so create any necessary and…
Tiago Mendes
  • 7
  • 1
  • 3
0
votes
2 answers

how to replace strings with path objects in python?

I have the following dictionary: config = { 'base_dir': Path(__file__).resolve(strict=True).parent.absolute(), 'app_dir': '/app' } I want to replace with the value of so I did this: for key, value in config.items(): …
syrkull
  • 2,295
  • 4
  • 35
  • 68
0
votes
0 answers

What is the reason for the 'list object is not callable' error here?

I'm trying to count the number of images inside a directory, sub-directories included. import pathlib data_dir = pathlib.Path('images') image_count =…
0
votes
1 answer

pathlib joinpath in pandas 3.7

I am trying to join a relative path to an absolute one. I am confused as to this behavior: from pathlib import Path path = Path("/an/absolute/path/test") path.joinpath("/../relative/path",…
michel
  • 282
  • 2
  • 17
0
votes
1 answer

Passing a list or tuple of unknown length to pathlib

This question is essentially two parts: how does Pathlib handle an unknown number of path segments as arguments and is it possible to pass a list or tuple into the constructor? The documentation for the Path class is as follows: class…
Anthony
  • 1,760
  • 1
  • 23
  • 43
0
votes
0 answers

Glob and pathlib Path.glob yield different results for same pattern

I'm trying to iterate over all directories (not recursively) inside a given directory. I have this file…
odradek
  • 993
  • 7
  • 14