FileNotFoundError: [Errno 2] No such file or directory: 'userDetails'
<p>My program needs to read an external Notepad (.txt) file which has usernames and passwords. (Both the program and the document are on my desktop.) I used this code to read it.</p>

<pre><code>file = open ("userDetails.txt", "r")

userDetails = file.readlines()

file.close()
</code></pre>

<p>But I keep getting this error:</p>

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

<p>I tried putting them in a folder together, but still got the same error.  Any ideas?</p>

<p>Try and specify the full file path to the text file when you open it:</p>

<p>i.e <code>/full/path/to/file.txt</code> such as <code>/Users/Harrison/Desktop/file.txt</code></p>

<p>Right now it's likely that Python is looking for the file in the default Python working directory.</p>

<p>Also, it's a better practice when working with files to open it like this:</p>

<pre><code>with open('/path/to/userDetails.txt') as file:
    file_contents = file.readlines()
    # or whatever else you'd like to do
</code></pre>

<p>... and <a href="http://docs.quantifiedcode.com/python-anti-patterns/maintainability/not_using_with_to_open_files.html" rel="nofollow">here</a> is an explanation as to why it's good to do it this way.</p>

<p>If you're having trouble locating the full file path, here's how you can do it for <a href="http://www.cnet.com/how-to/how-to-copy-a-file-path-in-os-x/" rel="nofollow">Mac</a> and <a href="http://www.pcworld.com/article/251406/windows_tips_copy_a_file_path_show_or_hide_extensions.html" rel="nofollow">Windows</a>.</p>

python