How can tkinter open with file directory of specific folder (also containing .py script) on any system?
<p>In other words if I pass off the folder to someone containing a .txt and .py, and the script is run on their machine from this folder, how can I ensure the file dialog will open with this folder to select the .txt without knowing the absolute path? Referring to <code>initialdir=</code>??</p>

<pre><code>from tkinter import filedialog
from tkinter import *

root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir='/python', title="Select file",
                                           filetypes=[("Text Files", "*.txt")])
print(root.filename)
</code></pre>

<p>You should use the <code>os</code> module and <code>os.getcwd()</code> to find the current working directory of the .py file</p>

<pre><code>import os
from tkinter import filedialog
import tkinter as tk

root = tk.Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",
                                           filetypes=[("Text Files", "*.txt")])
print(root.filename)
</code></pre>

<p>I would also suggest doing <code>import tkinter as tk</code> instead as importing everything may lead to naming conflicts if you are not careful, plus it's much easier to determine that what you are referring to came from the <code>tkinter</code> module when you prefix it with <code>tk</code></p>

python tkinter