0

So I need to parse and read files that have only "fip" or "frp" in them. Rather than just doing all full glob then using an if statment, I decided to search the webs on how I can achieve this. I stumbled on the answers here: Python glob multiple filetypes

Specifically modified my code to use a solution I found:

flist = [f for f_ in [odfslogs_p_handler.glob(e) for e in ('*frp*', '*fip*')] for f in f_]

The p_handler is a pathlib object. Now this code works. I just need some help understanding the wizardry behind it.

I know this is list comprehension but I've only dealth with simple examples. Can someone please explain to me why this works? Also can I chain in more patterns in the inner tuples? So lets say I wanna also parse .txt, and .csv. Is it just a matter of adding a comma and including those patterns inside the tuple ?

edo101
  • 629
  • 6
  • 17
  • An alternative would be the glob `*f[ri]p*`. – Klaus D. Jul 21 '20 at 02:28
  • I didn't know glob can handle regex?. Btw, is there a way to prevent it from capturing files that are not plain text files. Like if the file has *frip.zip or *frp.csv? I want to exclude any files that are not pure text files that have the chars frp/fip in them and or the one that end in .frp and .fip. No zip files and no other file types @KlausD. – edo101 Jul 21 '20 at 02:35
  • Globs do not support regular expressions, they have their own similar but very limited syntax. And no new questions as a comment please! – Klaus D. Jul 21 '20 at 02:43
  • @KlausD. please just this one time. I don't want to open up a whole new question for this? Feel like I'd be spamming – edo101 Jul 21 '20 at 02:44

0 Answers0