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 do I deal with pandas not being able to read type 'WindowsPath' from pathlib?

I'm trying to read .html files into pd.read_html(). However each .html file is within a different directory. So I've iterated over each directory and put the path/name + html_file_name in a list called html_paths. I want to iterate over this list…
0
votes
2 answers

Getting subdirectories recursively but only up to a level

I stumbled upon a very simple problem today. I have a directory structure like this: main_dir |_A |_X |_Y |_B |_X |_Y |_C |_X |_PP |_QQ |_Y |_PP I want to…
enterML
  • 2,110
  • 4
  • 26
  • 38
0
votes
1 answer

The code is not returning me the latest file

Although I have files in the directory, the code is not returning anything, can anyone help me? from pathlib import Path date_creation = lambda f: f.stat().st_ctime directory = Path('directory') files = directory.glob('*.py') sorted_files =…
Fulana
  • 57
  • 1
  • 7
0
votes
0 answers

Python 3.x Custom function chaining with an existing PATHLIB library Path() object

I have defined a custom function based on pathlib module in my Python (3.x) script: def mk_dir(self): self.mkdir(parents=True,exist_ok=True) my_path = Path('./1/2/3/') mk_dir(my_path) as a shorthand to create a new directory with…
Klaidonis
  • 559
  • 2
  • 6
  • 22
0
votes
3 answers

Change file extension from .tar.gz to .tgz in Python3

Using pathlib, is there a simple solution to change a file extension with two suffixes like ".tar.gz" to a simple suffix like ".tgz". Currently I tried: import pathlib src = pathlib.Path("path/to/archive.tar.gz") dst =…
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
0
votes
1 answer

Process multiple files in a folder using multiprocessing

I’m trying to read a bunch of files in a folder, process the content and save them. As I have a lot of files I need to parallelize the operation. Here is the code I tried, but when I run it, nothing happens, I don’t even get any error. It is just…
Silver Duck
  • 581
  • 1
  • 5
  • 18
0
votes
2 answers

Double backslashes for filepath_or_buffer with pd.read_csv

Python 3.6, OS Windows 7 I am trying to read a .txt using pd.read_csv() using relative filepath. So, from pd.read_csv() API checked out that the filepath argument can be any valid string path. So, in order to define the relative path I use pathlib…
FJSJ
  • 219
  • 3
  • 14
0
votes
0 answers

Name error on shutil and pathlib in cmd prompt

i am trying to look for a files in directory , copy their entire windows path into a list and copy the those files on the temp folder. with the below code i am only able to copy the file name. i have tried using pathlib module but it keeps throwing…
prdp13
  • 81
  • 1
  • 8
0
votes
2 answers

Removing python path hardcode and passing list

**Update 1/8/2019 0945 EST I have passed the script through the function given by bhakta0007 but received a path error "The system cannot find the path specified:". After review, I added the below statement to the end of the script to pass the list…
Adam
  • 35
  • 1
  • 5
0
votes
0 answers

Python PATHLIB on 3.5.2 & 3.6

I have two Python environment and using PATHLIB to construct references to files and for placement. When I run the code on 3.6 below is works fine. In 3.5 I can't get it to work, so I have to manually specify every path. I'm new to this whole world.…
Paul Z
  • 53
  • 6
0
votes
0 answers

regex windows path incomplete escape '\U'

I have a program that should be runnable on windows and linux systems. One function I use cuts off some root of a path and leaves me with the relative path. import re from pathlib import Path def relative_path(absolute_absolute, root_path): a =…
Soerendip
  • 7,684
  • 15
  • 61
  • 128
0
votes
1 answer

Python : Copy content of directory c:\FLASH to all connected USB flash drives

I need to copy some documents, folders and files from one directory on my local drive to all connected USB flash drives. When destination is also folder everything works but in case the destination is USB drive root then I always get error: Error:…
VACMA
  • 1
  • 1
0
votes
1 answer

Problems with pathlib iterdir()

For my project, I read a line of input from my text file that reads "D C:/Test/Project1". The D signifies to print the files in the directory, ignoring subdirectories. "R" signifies to print the files in the directory and every subdirectory. The…
0
votes
1 answer

how do I correctly use Path (PyCharm)

I'm using PyCharm 2018.1.4 If I write from pathlib import Path p = Path('.') this runs fine. On the other hand, if I write import pathlib p = Path('.') I get NameError: name 'Path' is not defined I thought by using import pathlib I'm importing…
Nick
  • 17
  • 1
  • 3
0
votes
2 answers

Iterate through a folder with the use of pathlib, I can´t open file with the use of path + value + string

I would like to iterate through a folder with the help of pathlib. The problem is, it seems, that I can´t combine a value with a string with the use of my path "folder". The following error appears: TypeError: unsupported operand type(s) for +:…
Nils
  • 409
  • 6
  • 16