Python Setting Base Directory

Moving and running script from different machines can result in additional files that the script requires to break if the paths are not exactly the same. In my previous post I have discussed python-dotenv. The Python script is importing a file that is vital for the script to successfully execute.

To ensure the file is always found in the same directory as where the script is we can set the base directory using the following

Now when referencing files in the script we can use the basedir variable.

That’s it, files will always be found and we can always write then to exactly where we like as everything is based off where the Python script is located.

Leave a Comment

Your email address will not be published. Required fields are marked *