Title: I m trying to search a string in multiple files and print them out in another file
Tags: python;regex;file
Question: I have multiple .txt files in a source folder of which i have given the path in "src" .I want to search strings which looks like "abcd.aiq" and print them in a file which i named as "fi".
I have written the following code and it doesnt print anything inside the file although it doesnt give any error.

```import glob
import re
import os
src = (C:\Auto_TEST\Testing\Automation")
file_array= glob.glob(os.path.join(src,".txt"))
fi= open("aiq_hits.txt","w")
for input_file in file_array:
    fo=open(input_file,"r")
    line=fo.readline()

    for line in fo:
        line=r.strip()

        x= re.findall('\S*.aiq\S*',line)

        line= fo.readline()
        for item in x:
            fi.write("%s\n" %item)
            fo.close()
            fi.close()
```

Comment: Your String is broken inside your `src` tuple => "`src = (C:\Auto_TEST\Testing\Automation")`" ***Note:** Technically it is not a tuple if you only have 1 item and it is not followed by a comma... `foo = ('bar',)`*

Comment: @Finwood: I think it's supposed to be "`line=line.strip()`" See OP's other; very-similar question: [Stack Overflow: I m searching for a specific string in multiple “.txt” files using python](http://stackoverflow.com/questions/29470178/i-m-searching-for-a-specific-string-in-multiple-txt-files-using-python).

Comment: Why `line = fo.readline()` in code-lines 9 and 16? And what is `r` in `line = r.strip()`? And why are you closing `fi` inside your loop?

Comment: Src tuple? It is the path of the folder where files to be checked are kept


Here is another answer: I suppose this is what you are trying:

```import glob
import re
import os.path

src = 'C:/Auto_TEST/Testing/Automation'
file_array = glob.glob(os.path.join(src,'*.txt'))

with open("aiq_hits.txt","w") as out_file:
    for input_filename in file_array:
        with open(input_filename) as in_file:
            for line in in_file:
                match = re.findall(r'\S*.aiq\S*', line)
                for item in match:
                    out_file.write("%s\n" %item)
```

Let me quickly describe the changes I've made:

Opening files directly is not always a good idea. If the script crashes, the opened ```file``` object isn't being closed again, which can lead to data loss.
Since PEP 343 Python has the ```with``` statement, wich is generally agreed on being a better solution when handling files.

Calling ```f.readline()``` multiple times results in the script skipping these lines, because ```for line in f:``` reads lines on its own.

Finally, after every matching item you found you've been closing both the input file and the output file, so further reading or writing isn't possible anymore.

Edit: If you might need to tweak your regex, this might be a useful resource.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/09/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><regex><file>', 'Title': 'I m trying to search a string in multiple files and print them out in another file', 'ViewCount': 73, 'id': 29544097, 'user_AboutMe': None, 'user_AccountId': 5996400.0, 'user_DisplayName': 'shashank jain', 'user_Reputation': 13.0, 'user_Views': 3.0, 'user_id': 4710913.0}}