0

I am new to Python and I just wrote a Python script in PyCharm that automates a configuration process, based on some input files. When I run it in PyCharm, it takes those files from the same folder as the script and delivers an outcome based on the running of the script. The problem is that I need to give this script to other people who don't know programming at all and once they install Python, they should be able to just download the script that I am giving them, put all the needed input files in the same folder and then double click on the script to receive the outcome. If I double click on the script in the folder where I have the PyCharm project, double-clicking will give me the output file, working perfectly. If I move the script in another folder (along with the input files), double clicking will not work. I am using Python 3.7.

nick
  • 1,310
  • 8
  • 15
TeoR
  • 5
  • 2
  • Look at `__file__` – Mad Physicist Jun 02 '19 at 08:37
  • Hi @MadPhysicist, thank you very much for your reply. As I mentioned, I am new to Python. Could you be so kind to give more details regarding this? Thank you. :) – TeoR Jun 02 '19 at 08:39
  • 1
    Possible duplicate of [How to properly determine current script directory?](https://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory) – melpomene Jun 02 '19 at 08:42
  • https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory – melpomene Jun 02 '19 at 08:43

1 Answers1

0

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

Then prepend dir_path to every file call you make.

Source: Find current directory and file's directory

Basile
  • 121
  • 6