Where do you save a notepad file so python can open it in the program?
<p>My program can't locate the file. How do I save the text file in the python directory on Windows? I've looked through other similar questions but cant find a basic guide to save into the same directory. Thanks for the help</p>

<pre><code>user_input = input("file name")
fh=open(user_input,"r",encoding="utf-8")
</code></pre>

<p>You are probably giving relative paths to <code>user_input</code>. Relative paths are resolved based on the <em>current working directory</em> (cwd). cwd is the directory from which python was started, which can be anything (not only where it is located), depending on how you start the script.</p>

<p>To find out what the cwd is:</p>

<pre><code>print(os.getcwd())
</code></pre>

<p>But instead of basing everything on CWD, I suggest you should do something else.</p>

<p>1st option: choose a different base directory:</p>

<pre><code>base_directory = 'C:\\My Favourite Directory'
user_input = input("file name")
file_path = os.path.join(base_directory, user_input)
fh = open(file_path, "r", encoding="utf-8")
</code></pre>

<p>2nd option: specify absolute paths as input:</p>

<p>When asked, for file name, type:</p>

<blockquote>
  <p>C:\My Favourite Directory\my file.txt</p>
</blockquote>

python file