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

It is possible to dump content of a text file into a Python list?

I have a directory of 50 txt files. I want to combine the contents of each file into a Python list. Each file looks like; line1 line2 line3 I am putting the files / file path into a list with this code. I just need to loop through file_list and…
uncrayon
  • 395
  • 2
  • 11
-1
votes
2 answers

Python Pathlib Strange Behaviour

I can find all the files in the subfolders of a folder path with specific filetypes in this way: list(Path(folder_path).glob('**/*.[jpg][jpeg][png]*')) But if I change the code to try and find other filetypes (like jfif or bmp), with some filetypes…
Cypher
  • 2,374
  • 4
  • 24
  • 36
-1
votes
2 answers

Checking if filename prefixes match parent directory prefix recursively with pathlib

I've written a script that uses pathlib to compare a list of files provided by the user to what is actually in a target directory. It then returns lists of files that were expected but not found, and files that were found but were not expected. It…
Paul
  • 25
  • 6
-1
votes
1 answer

Python, Reading Zip files of a subdirectory. Windows object is not iterable

I am trying to loop through my subdirectories to read in my zip files. I am getting error TypeError: 'WindowsPath' object is not iterable What i am trying: path = Path("O:/Stack/Over/Flow/") for p in path.rglob("*"): print(p.name) …
Jonnyboi
  • 505
  • 5
  • 19
-1
votes
1 answer

How can I check is each keys in this dict exist and that they all have a non empty value?

I have a toml file that looks like [default] conf_path = "d" prefix = "p" suffix = "s" I am turning this into a dict that looks like, {default: {'conf_path': 'd', 'prefix': 'p', 'suffix': 's'}} They key is default and the value is a dictionary. I…
uncrayon
  • 395
  • 2
  • 11
-1
votes
1 answer

I am getting the Path has no attribute join error

Here is the code: from pathlib import Path import sys # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent PROJECT_DIR = Path.join(BASE_DIR, "online_auction") # Quick-start development…
Bakanium
  • 1
  • 2
-1
votes
1 answer

Cannot define path: TypeError: unsupported operand type(s) for /: 'str' and 'str'

I've been facing an issue with defining a directory to use for the function open(). Unfortunately, it tells me that there is a TypeError: unsupported operand type(s) for /: 'str' and 'str'. How can I define in what folder to put the file without…
-1
votes
1 answer

Issues getting file extensions using os.path.splitext() method

I am working on a Flask blog application, and I am trying to separate a file extension from a filename. For example, for the filename "IMG_0503.jpg," I would like to get "IMG_0503" and ".jpg" separately. I tried the pathlib module and the…
Anna
  • 1
-1
votes
1 answer

Reading .csv and .xlsx in a directory using Data frame

How to read all files in a directory if the directory contains .csv and .xlsx files? I tried: read_files = Path(path).rglob("*.csv","*.xlsx") all_files = [pd.read_excel(file) for file in read_files] But it is not working. How can I achieve it?
Sriram
  • 169
  • 1
  • 9
-1
votes
2 answers

Linux vs. Windows Pathlib / OS Module

I am fairly new to programmer & 90% of my programming is in the data analyst space. Much of my work is reading in csv or excel files using the Pandas package. I program on a few different machines & use Dropbox to sync the csv files & then Github to…
keg5038
  • 341
  • 3
  • 13
-1
votes
1 answer

TypeError: join() argument must be str, bytes, or os.PathLike object, not 'tuple'

I want to process all the files in my path. If its a zip file, I want to extract its content as img. from pathlib import Path import pandas as pd import zipfile import os import sys import pathlib path = "./CODEX/input/" for filename in…
melololo
  • 161
  • 2
  • 3
  • 12
-1
votes
2 answers

using pathlib instead of os for identifying file and changing

I have tried to do the below using pathlib instaed of os. import sys import os folder = sys.argv[1] newfolder = sys.argv[2] for filename in os.listdir(folder): img = Image.open(f'{folder}{filename}') …
-1
votes
2 answers

Find a number within a list of paths

I am trying to create a file into the right directory based on a 4 digit int. Example: I'm creating an excel file and I want it to be placed into a folder that contains 1234 somewhere in the name like "Folder(1234)_do. I have the parent directory…
G_olof
  • 31
  • 6
-1
votes
1 answer

Why does pathlib.Path(".").parent not return ".."?

I cannot seem to get the parent of "." correctly using pathlib: >>> Path(".").parent PosixPath(".") Even worse, Path(".") is its own parent: >>> Path(".").parent == Path(".") True I would expect Path(".").parent == Path("..") instead - what am I…
bers
  • 4,817
  • 2
  • 40
  • 59
-1
votes
1 answer

Is there a way to check if a file has an extension, and if not append that extension?

My code is supposed to create .csv files and it does, but it doesn't always append the .csv extension to the end of the file even though it is a .csv. I just want to check the final output file before it is returned to the user, and append .csv if…