..the code that i have written already defines a file name..
The script I wrote, was on the premises that you want to write your selected "data", from each of the file, to a new file each.
So, if your intention, then is to use just one file, to get all the selected "data", from various files in the directory ( or directories ), since you said you already have a filename. Then, you might have to modify, your code a bit.
However, I might be wrong here. Please, if I may clear thing out.
How are you getting your new files?
Are you writing, all your output to a single file?
Or to a new file for EACH file you are reading from?