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

Getting an error while trying to pass arguments from command line

I am trying to create a python script file with multiple functions and pass 3 arguments to the main functions. These parameters are also passed onto other functions with the script. My code looks somewhat like this: import benepar import…
0
votes
0 answers

How can I glob all files (with and without extension) in python using pathlib and glob

I want to create a list of all files in a directory using pathlib's glob. The file names may have an extension (ex.: 'text.txt', 'abc.csv') or not ('text', 'abc'). I do realize that this can be handled by the same solution which is the answer to…
fotis j
  • 427
  • 1
  • 6
  • 14
0
votes
1 answer

Python - Create a subdirectory contains all directories and files in the parent directory

I want to create a subdirectory contains all directories and files in the parent directory. For example: There exists a parent directory and its subdirectories and files. /parent /parent/child1 /parent/child1/a.txt …
j94223
  • 5
  • 2
0
votes
1 answer

Shutil.move inside a recursive function

I wrote this function to achieve a similar behavior as in Windows when moving files and directories. In particular, objects should be overwritten. from pathlib import Path import shutil import os def move_anyhow(source: Path | str, dest: Path |…
Zio
  • 31
  • 3
0
votes
1 answer

Can't pip install pathlib on M1 Mac

When I run pip install pathlib==1.0.1 I get the following error: error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [20 lines of output] Traceback (most recent call last): …
Daniel Johnson
  • 193
  • 2
  • 14
0
votes
1 answer

I can't save the cleaned df to target directory

I am trying to remove duplicates from large files, but save those into a different directory. I ran the code below, but it saved them (overwrote) within the root directory. I know that if I switch to inplace='False' it won't overwrite those files in…
0
votes
0 answers

How can I find a file on the main OS/System PATH (not the PythonPath)?

How can I find a file the location of a file on the System PATH (i.e. in Windows %PATH% environment variable) with Python? This is not about the Python Path, the built-in sys.path, or the environment variable PYTHONPATH, but the PATH from the main…
LightCC
  • 9,804
  • 5
  • 52
  • 92
0
votes
2 answers

Python Pathlib escaping string stored in variable

I'm on windows and have an api response that includes a key value pair like object = {'path':'/my_directory/my_subdirectory/file.txt'} I'm trying to use pathlib to open and create that structure relative to the current working directory as well as…
Jamalan
  • 482
  • 4
  • 15
0
votes
0 answers

Python Pathlib finds a file but then looses it?

I need to find an excel file with certain nomenclature but I'm receiving an odd error: ppp = pathlib.Path('A:\\bsolute\path_to\dir_being_searched').glob('**/*edit.xlsx') for iii in ppp: print(iii.absolute()) This then returns one absolute path…
Messypuddle
  • 412
  • 6
  • 12
0
votes
1 answer

PathLib How to extract 2nd before last item in the path?

Example: >>> from pathlib import Path >>> p = Path("/home/user/Downloads/repo/test.txt") >>> print(p.stem) test >>> print(p.name) test.txt I would like this function to return repo. (2nd before last item in the path) I believe that os.basename is…
Katsu
  • 8,479
  • 3
  • 15
  • 16
0
votes
0 answers

Databricks Pyspark code with Substring and suffix(''), then List[] table names

OBJECTIVE: pull a list of table names from an Azure Gen2 container, then ingest CSVs into Delta Lake. So far I have this: In Databricks use %fs ls /mnt/myPath/ to list the CSV files in a table, then manually download that table to local Excel, then…
user3439308
  • 485
  • 1
  • 6
  • 11
0
votes
1 answer

Convert images in multiple subfolders into pdf

I am trying to deal with any image extension to convert any image to pdf (in any subfolder) Here's my try from pathlib import Path from PyPDF2 import PdfFileMerger import os BASE_DIR = Path.cwd() MAIN_DIR = BASE_DIR / 'MAIN' for subfolder in…
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
0
votes
1 answer

Anyone knows why my code will only find the files for the first item in the list and not others? i.e. it will filter files '*a.LOG.bz2' in filename

l='1001' dts_lst=list(pd.date_range(datetime.strptime('2022-03-15', '%Y-%m-%d'), Dt_now, freq='D').strftime('%Y-%m-%d')) # list of days p1=map(lambda x, y:Path(drive / x / 'foldera' / y / 'folderb' ), dts_lst, repeat(l)) to_search=['*a.LOG.bz2',…
0
votes
1 answer

Iterate over pathlib paths and python-docx: zipfile.BadZipFile

My python skills are a bit rusty since I recently primarily used Rstats. However I ran into the following problem, my goal is that I want to recursively iterate over all .docx files in a directory and change some of the core attributes with the…
Björn
  • 1,610
  • 2
  • 17
  • 37
0
votes
1 answer

How can pathlib read_text() method display German Umlaute correctly on Windows 10 Enterprise?

I have problems reading a text file textFile1 with the following content: Das erste Mal war noch in der Audition-Phase bei einem Screentest. Sie haben mir das alte Kostüm von einem meiner Vorgänger, Val Kilmer, gegeben. Es war verrückt. Ich weiss…
aurumpurum
  • 932
  • 3
  • 11
  • 27