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

Cant check if a file exists in a directory

When i try to check if a file exists making use of the pathlib.Path method ".exists()" or "is_file()" what i get is False. home = pathlib.Path.home() path1 = home / r"Desktop\Python 3\Libro de Python3\my_directory\file1.txt" path2 = home /…
Heijs7
  • 27
  • 6
0
votes
1 answer

what could cause this error : FileNotFoundError: [Errno 2] No such file or directory

I'm very new to coding and Python so I'm really confused by this error. Here's my code from an exercise where I need to find the most used word into a directory with multiples files: import pathlib directory =…
karl97100
  • 5
  • 1
  • 3
0
votes
1 answer

Python 3.6 looking in wrong module/library ( _io.TextIOWrapper ) for function that should be called from pathlib

I am getting the following error from the code that follows, below: AttributeError: '_io.TextIOWrapper' object has no attribute 'write_text' Code: import pathlib output_filepath = pathlib.Path(r'/home/john/somedir/data/somefilename.csv') with…
JohnCroc
  • 79
  • 1
  • 9
0
votes
1 answer

Weird path behavior when using os.path and pathlib Mac OSX Catalina

I have an image called image1.png its real path on my macbook is : /Users/emadboctor/Desktop/images/image1.png and the image is found by calling: images = os.listdir('/Users/emadboctor/Desktop/images/image1.png') let's say I want to get the…
user12690225
0
votes
0 answers

creat a new folder and file and make them readable in all the application scripts PyQt5

I am trying to create A new folder and new data base file to be the startup page of an application, what i am looking for is to make the folder name and DB file name to be seen (globally)in all scripts. file 1 import os import pandas as pd from…
Sam Fathy
  • 3
  • 1
  • 3
0
votes
3 answers

Using recursive file search and exclude startswith() with pathlib

I want to search recursive for all files in all folders with pathlib, but I want to exclude hidden system files that start with '.' (like '.DS_Store') But I can't find a function like startswith in pathlib. How can I achieve startswith in pathlib? I…
BenjaminK
  • 653
  • 1
  • 9
  • 26
0
votes
1 answer

Deleting files from a directory using a csv file in python

I am looking to delete files from multiple folders using a .csv file. The csv file contains a list of file names that need to be deleted(example: Box4, 60012-01). How the data is stored is in multiple folder and also had additional extension…
Sameep Shah
  • 23
  • 1
  • 6
0
votes
1 answer

Python - Looping through files in directory freezes mid-way

I have a simple script set up using ImageMagick to delete all images in a directory that aren't of size 157x200 pixels: import subprocess, os, sys from tqdm import tqdm from pathlib import Path def delete_opaque_files(): pathlist =…
Colin
  • 117
  • 8
0
votes
0 answers

Time complexity for listing directories and subdirectories

I used something like this to go through all subdirectories in every directory for .txt files: for txt_file in pathlib.Path(path).glob('**\*.txt'): ... But I'm not sure how to determine worst time complexity for it.
0
votes
1 answer

Subprocess.call() cwd cannot set from text loaded value

TL;DR subprocess.call(cwd=filepath) does not work when I set the filepath variable from a text file, but does work when I set it manually using an identical path. More Info When I use subprocess.call I specify the cwd of the command with a string…
Monty
  • 1
0
votes
1 answer

Using Path to check if file exists when running script outside of the directory

So I currently use Path to check if a file exists from pathlib import Path if Path("main.conf").is_file(): pass else: setup_config() While this works as long as I'm in the directory where I'm running the script, I'd like to make it work…
chomes
  • 59
  • 7
0
votes
1 answer

How can I compress jpegs in Python more efficiently?

I'm working with thousands of large image files in a regularly updated library. The following script does the job (on average reduces my file size ~95%) but costs me around 25 seconds to compress one image. Obviously, I can just let the script run…
0
votes
2 answers

Find files within a changed subdirectory in Python

I have a text-file full of filenames. Like: C:\Folder\Subfolder_01\file_1001.csv C:\Folder\Subfolder_02\file_3030.xls ... I want to check whether the files still exists (which is easy) or if the name of the subfolder has changed. The name of some…
rhombuzz
  • 97
  • 10
0
votes
1 answer

Get user in path regex or pathlib Python

Given the examples /foo/bar/foobar/user1/somethingelse /foo/bar/barfoofoo/user2/blabla/blah/bluh I'm trying to extract user1 and user2. I know that both of their grandparents is bar, and I know that they're the 4th item in the path. The length…
Geoffrey Negiar
  • 809
  • 7
  • 28
0
votes
1 answer

recursive searching with pathlib - python

Say I have these files /home/user/one/two/abc.txt /home/user/one/three/def.txt /home/user/one/four/ghi.txt I'm trying to find ghi.txt recursively using the pathlib module. I tried: p = '/home/user/' f = Path(p).rglob(*i.txt) but the only way I…
HappyPy
  • 9,839
  • 13
  • 46
  • 68