title: File not found when opening a file whose name I get from os.listdir
body: <p>I'm trying to read the first 20 characters of several files that I have in a folder using the code shown below:</p>
  
  <pre><code>import os
  path = raw_input("Enter the path: ")
  listing = sorted(os.listdir(path))
  for infile in listing:
      print "current file is: " + infile
      with open(infile) as fl: # Use fl to refer to the file object
          #print infile
          data = fl.read(20)
          print data
          fl.close()
  </code></pre>
  
  <p>But I'm getting this error:</p>
  
  <pre><code>Enter the path: /home/david/dat_files_new
  
  current file is: raw_57_0000.dat
  # == pyFAI calibrati
  current file is: raw_57_0001.dat
  
  ---------------------------------------------------------------------------
  IOError                                   Traceback (most recent call last)
  &lt;ipython-input-53-7292764186b6&gt; in &lt;module&gt;()
        7 for infile in listing:
        8     print "current file is: " + infile
  ----&gt; 9     with open(infile) as fl: # Use fl to refer to the file object
       10         #print infile
       11         data = fl.read(20)
  
  IOError: [Errno 2] No such file or directory: 'raw_CL20_57_0001.dat'
  </code></pre>
  
  <p>I can list all the files in the directory if I comment the with statement and everything that it is below.</p>
  
  <p>This is what the code does:</p>
  
  <ul>
  <li><p>Sort the files within a directory --> done</p></li>
  <li><p>Print name of first file --> done</p></li>
  <li><p>Read the first 20 characters of the first file --> done</p></li>
  <li><p>Print name of seconde file --> done</p></li>
  <li><p>Read the first 20 characters of the second file --> failed</p></li>
  <li><p>Read the first 20 characters of the second file </p></li>
  </ul>