Search code examples
python-3.6relative-path

Making relative file paths for outputs


After looking at several similar questions on making relative file paths, I have some confusion on how to apply a relative file path for the outputs in my script. I have an application folder called "Master Map Change Report App" in which my script and outputs currently sit. enter image description here enter image description here Right now this app sits on my local C: drive, but I need to move it to a shared drive so that other people can use it. I have all of the outputs going to 'reports' folder (see below). Currently the path to that folder looks like this:

 report = "C:/Workspace/Sandbox/MapChangeProject/Master Map Change Report App/reports/map_change_report_{}.xls".format(today).

What I want is something like this:

 report = ".../Master Map Change Report App/reports/map_change_report_{}.xls".format(today).

Of course, that doesn't quite work. What do I need to do here so that my outputs will always be within the 'Master Map Change ReportApp/reports' folder no matter where that folder is moved?


Solution

  • Your script is at "Master Map Change Report App". So your relative path for outputs should be "reports/map_change_report_{}.xls".format(today). You may need:

    import os
    dirname = os.path.dirname(os.path.realpath('__file__'))
    filename = os.path.join(dirname, 'reports','map_change_report_{}.xls'.format(today))