title: Python can't open file "No such file or directory"
body: <pre><code>def main():
      fh = open('lines.txt')
      for line in fh.readlines():
          print(line)
  
  if __name__ == "__main__": main()
  </code></pre>
  
  <p>Directory files</p>
  
  <p><a href="https://i.stack.imgur.com/yNd7C.png" rel="noreferrer"><img src="https://i.stack.imgur.com/yNd7C.png" alt="enter image description here"></a></p>
  
  <p>I am on <code>for-working.py</code> file, and am trying to access the <code>lines.txt</code> file within the same working directory. But I get error</p>
  
  <blockquote>
    <p>No such file or directory: 'lines.txt'</p>
  </blockquote>
  
  <p>Does python need to have an absolute path when opening files?</p>
  
  <p>why doesn't this relative path work here?</p>
  
  <p>Running python 3.6</p>
  
  <p><strong>EDIT ^1</strong> I'm running visualstudio code with the python package extension by Don Jayamanne, and "Code Runner" package to compile/execute python code</p>
  
  <p><strong>EDIT ^2</strong> Full error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files\07 Loops\for-working.py", line 11, in &lt;module&gt;
      if __name__ == "__main__": main()
    File "c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files\07 Loops\for-working.py", line 7, in main
      fh = open('lines.txt', 'r')
  FileNotFoundError: [Errno 2] No such file or directory: 'lines.txt'
  </code></pre>
  
  <p><strong>EDIT ^3</strong> checking sys.path</p>
  
  <pre><code>import sys
  print(sys.path)
  </code></pre>
  
  <p>produces this information:</p>
  
  <pre><code>['c:\\www\\Ex_Files_Python_3_EssT(1)\\Ex_Files_Python_3_EssT\\Exercise Files\\07 Loops', 
  'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\Kagerjay\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
  </code></pre>
  
  <p><strong>EDIT ^4</strong> checking os.getcwd()</p>
  
  <p>Running</p>
  
  <pre><code>import os
  print(os.getcwd())
  </code></pre>
  
  <p>Produces</p>
  
  <pre><code>c:\www\Ex_Files_Python_3_EssT(1)\Ex_Files_Python_3_EssT\Exercise Files
  </code></pre>
  
  <p><strong>Well its definitely not in the right subdirectory (needs to <code>cd 07 loops</code> folder, that narrows the issue down</strong></p>
  
  <p><strong>EDIT ^5</strong> what is in lines.txt file</p>
  
  <p>My lines.txt file i am opening looks like this. No extra whitespace or anything at start</p>
  
  <pre><code>01 This is a line of text
  02 This is a line of text
  03 This is a line of text
  04 This is a line of text
  05 This is a line of text
  </code></pre>
  
  <p><strong>IN SUMMARY</strong></p>
  
  <p>Visual studio code's Code runner extension needs to be tweaked slightly to open files within a subdirectory so any of the below answers would provide a more robust solution to be independent of any extension / dependencies with the IDE</p>
  
  <pre><code>import os
  print(os.getcwd())
  </code></pre>
  
  <p>Is most useful for diagnosing problem to the current directory python interpreter sees</p>