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>

<p><a href="https://docs.python.org/3/library/os.html#os.listdir" rel="nofollow"><code>os.listdir</code> only returns the file names</a>, not the qualified paths. So to make this work, your <code>open</code> needs to be opening the qualified path (constructed with <code>os.path.join("/home/new", file)</code>), not just <code>file</code>.</p>

python os.system