pathlib

Pathlib – print contents of a folder

Print contents of a folder Question: Here is my example. Ex: I have a folder that contains another 3 folders (FoldA, FoldB, and FoldC), a .txt file, and a .png file. I have the following working code which works to print the contents a folder or directory. import pathlib rd = pathlib.Path("E:\Location\MainFolder") for td in …

Total answers: 1

Has Path from pathlib a function which can check subfolders?

Has Path from pathlib a function which can check subfolders? Question: I want to find all python files in a folder which are not located in a subfolder (e.g. ‘.ipynb_checkpoints’). My current solution is from pathlib import Path rootdir = Path(‘/home/my_path’) # For absolute paths instead of relative the current dir file_list = [ f …

Total answers: 1

How do I download a folder from AWS S3 with CloudPathLib from a public bucket?

How do I download a folder from AWS S3 with CloudPathLib from a public bucket? Question: I’m trying to download a folder from a public AWS S3 bucket using the Python library cloudpathlib. My code looks like this: from cloudpathlib import CloudPath path = r"C:somepathtofolder" url = "s3://some-example-bucket/folder/" cloud_path = CloudPath(url) cloud_path.download_to(path) Really straight forward. …

Total answers: 3

Get directories only with glob pattern using pathlib

Get directories only with glob pattern using pathlib Question: I want to use pathlib.glob() to find directories with a specific name pattern (*data) in the current working dir. I don’t want to explicitly check via .isdir() or something else. Input data This is the relevant listing with three folders as the expected result and one …

Total answers: 2

How to work with regex in Pathlib correctly?

How to work with regex in Pathlib correctly? Question: I want find all images and trying to use pathlib, but my reg expression don’t work. where I went wrong? from pathlib import Path FILE_PATHS=list(Path(‘./photos/test’).rglob(‘*.(jpe?g|png)’)) print(len(FILE_PATHS)) FILE_PATHS=list(Path(‘./photos/test’).rglob(‘*.jpg’))#11104 print(len(FILE_PATHS)) 0 11104 Asked By: Piter || Source Answers: Get list of files using Regex import re p = …

Total answers: 2

How do you read lines from all files in a directory?

How do you read lines from all files in a directory? Question: I have two files in a directory. I’d like to read the lines from each of the files. Unfortunately when I try to do so with the following code, there is no output. from pathlib import Path p = Path(‘tmp’) for file in …

Total answers: 3

How can I output paths with forward slashes with pathlib on Windows?

How can I output paths with forward slashes with pathlib on Windows? Question: How can I use pathlib to output paths with forward slashes? I often run into programs that accept paths only with forward slashes, but I can’t figure out how to make pathlib do that for me. from pathlib import Path, PurePosixPath native …

Total answers: 1

Pathlib and stem – Attributerror

Pathlib and stem – Attributerror Question: as part of a code I have function as follow: def match_output(orig_path: Path,lines: Iterable[str],stem: str, delim: str,delim_pred: Callable[[int], bool],) -> Iterable: n = 0 path = orig_path.with_stem(f'{orig_path.stem}_{stem}’) with path.open(‘w’) as f: for line in lines: n_delim = line.count(delim) matched = delim_pred(n_delim) if matched: f.write(line) n += int(matched) yield logger.info(f’Number …

Total answers: 1

What is the difference between pathlib glob('*') and iterdir?

What is the difference between pathlib glob('*') and iterdir? Question: Suppose I’m writing code using pathlib and I want to iter over all the files in the same level of a directory. I can do this in two ways: p = pathlib.Path(‘/some/path’) for f in p.iterdir(): print(f) p = pathlib.Path(‘/some/path’) for f in p.glob(‘*’): print(f) …

Total answers: 2

Is there a Pathlib alternate for os.path.join?

Is there a Pathlib alternate for os.path.join? Question: I am currently accessing the parent directory of my file using Pathlib as follows: Path(__file__).parent When I print it, and this gives me the following output: print(‘Parent: ‘, Path(__file__).parent) #output /home/user/EC/main-folder The main-folder has a .env file which I want to access and for that I want …

Total answers: 6