Search all files in a folder
<p>I want to search "word" in many files in a folder. </p>

<p>I have already : </p>

<pre><code>route=os.listdir("/home/new")
for file in route:
</code></pre>

<p>This does not work : </p>

<pre><code> f = open ('' , 'r')
 for line in f : 
</code></pre>

<p>I tried this :</p>

<pre><code>for file in route:
    f = open(file, 'r')
    for line in f:
        if word in line:
            print(file)
            break
</code></pre>

<p>but I have an error :</p>

<pre><code>f=open( file ,'r')
IOError: [Errno 2] No such file or directory: file.txt
</code></pre>

<p>When I delete file.txt, next file , I receive the same error.</p>

<pre><code>for file in filelist:    
    f = open(file,"r")
    data = f.read()
    rows = data.split("\n")
    count = 0
    full_data = []
    for row in rows:
        split_row = row.split(",")
        full_data.append(split_row)
    for each in full_data:
        if re.search("word", each) is not None:
            count += 1
</code></pre>

<p>Something like this, although your question is not at all specific about whether you want to count, return where word was found, change word to something, etc. so feel free to edit it as you see fit</p>

<p>(This code works for .csv as you can probably tell)</p>

python os.system