View Single Post in: Python: specifying a directory for an output file

Deceased
#2 Old 25th Jul 2018 at 4:59 AM
Quote: Originally posted by LynneM
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), logname)

Yup, that will include the filename if you are using a compiled script in a .zip or .ts4script file. The zip file is kind of considered part of the path for the Python loader functions.

The way I've chosen to handle this lately is just to take the dirname of the path if the path ends in .zip or .ts4script, something like this:

Code:
pathname = os.path.dirname(os.path.realpath(__file__))
if pathname.lower().endswith('.zip') or pathname.lower().endswith('.ts4script'):
    pathname = os.path.dirname(pathname)
filename = os.path.join(pathname, logname)


Works fine and is much easier to read than the method I used previously involving string array offsets. This will only remove that filename from the path if the script is in a zip/ts4script file, so this will still work if the script is uncompiled in a Scripts folder as well.