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

Having trouble trying to upload images on instagram

first I'm sorry for my english!!I have a problem with my code because I want it to automatically upload an image to my Instagram profile but it doesn't work because it repeats the same photo over and over but I don't want that to happen!!Can someone…
olamundo97
  • 25
  • 4
0
votes
1 answer

Error with word document after modifying with pathlib

This is my code: from pathlib import Path doc_path = Path.home() / "word.docx" doc_path.touch() with doc_path.open(mode="w", encoding = "utf-8") as file: file.write("hello") utf-8 is the default setting of word on my computer but whenever I…
Despereaux
  • 201
  • 2
  • 10
0
votes
2 answers

File Path Concatenation

I need to build a Windows (only) file path from a combination of a UNC path passed to my script as a parameter, an API call, and a calculated date. I'm having a horrible time of it, mostly caused by Windows' use of a backslash character to delimit…
Brian
  • 37
  • 5
0
votes
0 answers

Constructing path with pathlib for import - not working

I am working on some code that requires importing a module from a directory several levels up and down. Here is the code: import os import pathlib # ## Print path to this file my_name = pathlib.Path(__file__) my_path =…
Ubaidul Khan
  • 131
  • 3
  • 13
0
votes
1 answer

Recursively find and copy files from many folders

I have some files in an array that I want to recursively search from many folders An example of the filename array is ['A_010720_X.txt','B_120720_Y.txt'] Example of folder structure is as below which I can also provide as an array e.g ['A','B'] and…
Kamikaze K
  • 181
  • 1
  • 11
0
votes
2 answers

Python FileNOtFound Error [WinError3] on Windows Server 2012 Machine

I wrote the below code to be able to iterate through all files, sub-folders and folders given a file path. However, when I run the code it keeps giving me file not found error. It's also worth mentioning that when I run same code on my mac, it was…
pyzer
  • 122
  • 7
0
votes
2 answers

How to loop through multiple paths using pathlib

Trying to list, for example, all the jpgs on two drives. Single drive search looks like: from pathlib import Path for path in Path("/Volumes/MasterOne").rglob('*'): for file_name in path.glob('*.jpg'): print(file_name) But am not…
Woolwit
  • 89
  • 8
0
votes
1 answer

Hello , why does it return False for file exists() when im using relative path but return True when path is absolute? thanks

import pathlib home1 =pathlib.Path("/Users/sergii/Desktop/hello.txt") print(f"home 1 is located {home1.cwd()}") #home 1 is located /Users/sergii/Documents print(f"home 1 exists {home1.exists()}") #home 1 exists True print(f"home 1 is a file…
Sergii
  • 1
  • 1
0
votes
1 answer

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect (error while trying to access an image dataset from drive)

I am trying to access a dataset (and count the number of images) saved in my google drive through the following code in jupyter notebook, but I keep getting this error. I cannot use google colab for security reasons.The dataset has 17 folders and in…
0
votes
1 answer

Sampling files from multiple folders, getting TypeChoice error

I'm currently trying to figure out how to sample multiple audio files (sample size of 50+) from a span of multiple folders to eventually use to train a model. Obviously doing this by hand would be very tedious, so I'm trying to figure out how to…
Daveguy
  • 255
  • 3
  • 11
0
votes
1 answer

recursively get all files except the hidden ones with pathlib

I would like to use pathlib to recursively iterate over all files of a given path, except those which are hidden or are in hidden directories. E.g., from | |- a.txt |- .b.txt |- a/ | |- c.txt | |- .d.txt | +- c/ | +- e.txt |- .b/ +-…
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
0
votes
1 answer

Using pathlib.Path with spark.read.parquet

Is it possible to use pathlib.Path objects with spark.read.parquet and other pyspark.sql.DataFrameReader methods? It doesn't work by default: >>> from pathlib import Path >>> basedir = Path("/data") >>> spark.read.parquet(basedir /…
ei-grad
  • 792
  • 7
  • 19
0
votes
1 answer

Does Path Object have builtin method to test if directory is in the Path

Is there a way to check if a directory is in the path of a python Path object. I know I can test it by converting it to a string like 'dir' in str(Path('/dir/dir2/dir3')) or 'dir' in Path('/dir/dir2/dir3').parts but I'm hoping there is a built in…
Matthew Barlowe
  • 2,229
  • 1
  • 14
  • 24
0
votes
0 answers

Check if files from lists of files exists

I have photos spread across folders and subfolders in my local directory. Besides that, I have a dataframe that contains names and paths of those photos. I would like to cross check (1) if the path/name exists in df and the photo doesn't exist, and…
KayEss
  • 419
  • 4
  • 18
0
votes
2 answers

How to use separators in pathlib to acces another file/folder

I have a script that changes the Windows background image considering the current time of the day (day or night). I wanted to make this script scalable in order to be used on another computer without any change in the code. from datetime import…
instaspam
  • 32
  • 2
  • 8