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><code>os.listdir</code> only returns filenames, to get the full path you need to join that filename with the folder you're reading:</p>

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

python io