reading all the file content in directory
<p>I have directory <code>poem</code> which contains 50 files and I want to read them all.</p>

<pre><code>for file in os.listdir("/home/ubuntu/Desktop/temp/poem"):
    print file
    f = open(file, 'r')
    print f.read()
    f.close()
</code></pre>

<p>This code reads file name of all the files in directory. 
But it fails at </p>

<pre><code>f = open(file, 'r')
</code></pre>

<p>saying </p>

<pre><code>IOError: [Errno 2] No such file or directory: '32'
</code></pre>

<p>you are searching file in current join file path with directory folder.</p>

<pre><code>import os

for i in os.listdir("/home/ubuntu/Desktop/temp/poem"):
    if os.path.isfile(os.path.join("/home/ubuntu/Desktop/temp/poem",i)):
        print os.path.join("/home/ubuntu/Desktop/temp/poem",i)
        f=open(os.path.join("/home/ubuntu/Desktop/temp/poem",i),"r")
        print f.readlines()
        f.close()
</code></pre>

python io