How to count RNA sequencereads using custom made python scripts?
What software are you using for the next step?  DESeq in R will do this filtering for you in one line.
I think a more typical way to do this would be to make your entire gene count matrix, and then remove rows where the max value for that row is &lt; 5.  Your way, you have to go through each file twice; once to know which genes to keep, and then again to actually remove lines.