Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Test Subject
Original Poster
#1 Old 24th Jul 2018 at 11:16 PM
Python: specifying a directory for an output file
Hi, all -

I'm new to modding and Python, and this site! Thanks for all the great info you provide here.

A friend has asked me to make him a script that outputs some data on his sims to a file. I've figured out the general idea thanks to various tutorials and have created a ts4script that runs and doesn't make the game crash , but I'm stuck on one thing. I can't figure out how to correctly specify the directory for the output file. Through some searching on this site I found this thread by scumbumbo:

http://www.modthesims.info/showthre...ght=output+file

And looking at the python file, I see this is how the filename and path are being set up:

filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), logname)

When I use that in my own script, the resulting path includes my ts4script file. In other words, I get this for my filename:

…\Electronic Arts\The Sims 4\Mods\Lynne_SimStuff.ts4script\simstuff.txt

So of course the write fails.


Can anyone help me figure out what I'm doing wrong? I'm also not real clear on what os.path.realpath(__file__) does, even after googling various Python sites.

TIA,
Lynne
Advertisement
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.
Test Subject
Original Poster
#3 Old 26th Jul 2018 at 2:05 AM
Yep, that did it! Thanks so much for the help!
Back to top