with pathlib.Path.relative_to we can get relative paths with: But what if i don’t know the exact path of the parent directory (but i do know its name)? for example: Path(“/path/to/foo/bar/thing”) relative to “foo” => PosixPath(‘bar/thing’) Path(“/path/to/foo/…
Tag: pathlib
python Path from pathlib module doesn’t add path separator
I have the following issue using Path from the pathlib library: I have tried with Path itself and manually giving it a path that I know exists: I was wondering why Path doesn’t simply add the backslash at the beginning? Is there a better way to build my path with the needed path separator from a list? I…
Doctest not working in Sphinx, cannot import python files Python
I have a problem trying to run doctest from the Sphinx Tutorial. I have the directory tree below but I cannot run a doctest for lumache.py. How would be able to make it so that lumache.py is importable with the pathlib function in conf.py which is sys.path.insert(0, pathlib.Path(__file__).parents[2].resolve()…
Python pathlib.Path library always cuts last folder from the path and adds it to the name of created file
So here is the problem, I have some path which is like this: Then I use this to “make” it win path: So now every time I use it, to create some other files inside of path directory, my files are created inside of “category” folder with prefix UFF, so file names are: instead of For creat…
iterating over folders executing a fuction at each 2 folders
I have a function called plot_ih_il that receives two data frames in order to generate a plot. I also have a set of folders that each contain a .h5 file with the data I need to give to the function plot_ih_il… I’m trying to feed the function two datasets at a time but unsuccessfully. I’ve be…
How to remove a file from Path object in Pathlib module?
I have a Path Object representing C:UsersusersDownloadsimg.jpg. How do I get it so the Path only represents C:UsersuserDownloads? I don’t want to delete the file, but rather go back in the Path object itself. Answer I would utilize the PurePath class within pathlib as follows: This yields: PureWindowsPa…
Airflow issue with pathlib / configparser – ‘PosixPath’ object is not iterable
I am trying to containerize my airflow setup. I’ve been tasked to keep the environment the same, just move it into a docker container. We currently have Airflow and all our dependencies installed within a anaconda environment. So what I’ve done is created a custom docker image that installs anacon…
How to replace characters and rename multiple files?
I have a bunch of pdf files which has the file name as follows: AuthorA_2014_ This is a good article BIsanotherAuthor_1994_ Gr8 artcle CIsFatherOfB_1994_Minor article but not bad And so on. I would like to change the name of the files to this format: AuthorA2014This is a good article BIsanotherAuthor1994Gr8 a…
How to rename all files to include the directory name?
I’m trying to use a For loop in the code below to go through a list of files and rename them with the file directory’s name. Answer Use pathlib Path.rglob: This is like calling Path.glob() with ‘**/’ added in front of the given relative pattern: .parent or .parents[0]: An immutable seq…
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: Here’s the error I get: What could cause this kind of error? Answer here’s what i guet what could…