title: Python get path to executable file?
body: <p>I'm very new to Python programming and I've cobbled together some code that will hopefully automatically rename a bunch files in a specific folder. I need to run this program on a computer without Python installed (it's not connected to the internet and I can't install python on it) so I've converted the .py file to a .exe file using auto-py-to-exe. The .py file works on my laptop, so the problem only shows up when I try to run the .exe file.</p>
  <p>I use the path to the folder that the script is located in throughout the code so at the beginning of the script, I save the filepath as &quot;path&quot;:</p>
  <pre><code>path = os.path.dirname(os.path.realpath(__file__))
  </code></pre>
  <p>However, when I convert the .py file to a .exe file and move that .exe file to another folder, the filepath that is saved in the 'path' variable is in a temp folder (probably the output location of the file converter). I tested it by isolating the part of the script that gets the filepath and making a .exe file to print just that filepath.</p>
  <p><strong>Is there a different way that I can get the filepath of the executable file?</strong> I don't want to hardcode the filepath into the program because people constantly reorganize the folders on that computer and I want this program to be flexible.</p>
  <p>Here is the rest of the code if it's needed for context. Thank you and I'm sorry if this is a really basic question :)</p>
  <pre><code>#  This section of code imports the modules used below
  import os
  import tkinter as tk
  from tkinter import simpledialog
  from tkinter import messagebox
  
  #  Setting global variables
  path = os.path.dirname(os.path.realpath(__file__)) + &quot;/&quot;  # This sets the filepath to the current folder
  
  #  Choosing the dough
  dough_input = tk.Tk(className='Select dough')
  dough_input.geometry('400x200')
  
  # This is the code that creates the dough selection box. Any new types can be added here using the template below:
  dough = tk.StringVar()
  radiobutton_1 = tk.Radiobutton(dough_input, text='a', variable=dough, value=&quot;a&quot;, tristatevalue=0)
  radiobutton_1.pack()
  radiobutton_2 = tk.Radiobutton(dough_input, text='b', variable=dough, value=&quot;b&quot;, tristatevalue=0)
  radiobutton_2.pack()
  radiobutton_3 = tk.Radiobutton(dough_input, text='c', variable=dough, value=&quot;c&quot;, tristatevalue=0)
  radiobutton_3.pack()
  radiobutton_4 = tk.Radiobutton(dough_input, text='d', variable=dough, value=&quot;d&quot;, tristatevalue=0)
  radiobutton_4.pack()
  
  
  # This section of code saves the dough chosen and closes the pop-up window.
  def submitfunction():
      global bread
      bread = dough.get()
      dough_input.destroy()
  
  
  submit = tk.Button(dough_input, text='Submit', command=submitfunction)
  submit.pack()
  
  # This section of code triggers the pop-up window for the dough selection.
  dough_input.mainloop()
  
  #  Inputting the first number
  root = tk.Tk()
  root.withdraw()
  user_inp = simpledialog.askinteger(&quot;Number Input&quot;, &quot;Input first number:&quot;)
  i = int(user_inp)
  
  #  Defining the file rename function
  def rename():
      global i  # use the variable  &quot;i&quot; defined above (The number that the user inputs)
      for filename in os.listdir(path):  # for each file in the folder specified above do the following
          if &quot;.sur&quot; in filename:
              my_dest = str(bread) + &quot; &quot; + str(i).zfill(4) + &quot;.txt&quot;  # sets the new filename
              my_source = path + filename  # Defines the old filepath to the file
              my_dest = path + my_dest  # Defines the new filepath to the file
              os.rename(my_source, my_dest)  # rename function
              i = i + 1  # advances the program down the list
      messagebox.showinfo(&quot;Success&quot;, &quot;All files have been renamed successfully!&quot;)  # confirmation message!
  
  
  # This function asks the user if the information they've input is correct. Once the user clicks &quot;ok&quot; the program runs
  # the rename function.
  def sanity_check():
      file_list = os.listdir(path)
      j = 0
      for g in file_list:
          if &quot;.txt&quot; in g:
              j += 1
  
      # This section of code does some calculations and creates a &quot;sanity check&quot; for the user. It takes the number that
      # the user inputted above and sets that as the first number &quot;i&quot;. Then it counts the number of files that
      # meets the above criteria and sets that to the number of files. It uses that number of files to calculate the
      # last number in the list. It then concatenates all this information and asks the user to 
      # confirm. If the user presses &quot;ok&quot; then the program
      # proceeds with the rename. If the user presses cancel, the program aborts.
  
      first_num = i
      num_files = j
      length_files = int(num_files) - 1
      last_file = i + length_files
      last_num = last_file
      check = str(
          &quot;There are &quot; + str(num_files) + &quot; files in this folder. The dough analyzed is &quot; + str(
              dough) + &quot;. The first &quot;
                         &quot;number is &quot; + str(first_num).zfill(
              4) + &quot; and the last number is &quot; + str(
              last_num).zfill(4) + &quot;. Is this correct?&quot;)
  
      # This is the section of code that asks the user to verify the numbers.
      confirmation = messagebox.askokcancel(&quot;Sanity Check!&quot;, check)
      if confirmation == False:
          messagebox.showerror(&quot;&quot;, &quot;Program Cancelled&quot;)
      else:
          rename()
  
  
  # This is the command that runs the program.
  sanity_check()
  </code></pre>