1

Is it possible to append a pathlib.Path generator or, combine two Paths?

from pathlib import Path paths = Path('folder_with_pdfs').glob('**/*.pdf') paths.append(Path('folder_with_xlss').glob('**/*.xls')) 

With this attempt you'll get:

AttributeError: 'generator' object has no attribute 'append' 

1 Answer 1

1

That's because Path.glob returns a generator, i.e an object that returns values when next is called which has absolutely no idea what appending is.

You have two options here, if you require a list wrap the paths in a list call:

paths = list(Path('folder_with_pdfs').glob('**/*.pdf')) paths.append(list(Path('folder_with_xlss').glob('**/*.xls'))) 

(Even though extend is probably what you're after here.)

this of course defeats the purpose of the generator.

So, I'd suggest using something like chain and creating a generator that will combine them and yield from them one at a time:

from itertools import chain p1 = Path('folder_with_pdfs').glob('**/*.pdf') p2 = Path('folder_with_xlss').glob('**/*.xls') paths = chain(p1, p2) 

Then iterating over paths as required while keeping the memory footprint down.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.