Writing to file inside p3d

Hey everyone,

In my game I use a text file to store some config variables/controls. After I pack the game to a .p3d file, reading the file works fine, but writing to it produces an error:

IOError: Could not open config/config.txt for writing

How can I write to this file?

Thanks in advance.

Have you checked if the “config” directory exists first?

For the record, you cannot replace a file inside a .p3d package.

I know it exists because I can read from the file using the exact same path.

Is writing to the file using open(“config/config.txt”, “w”) replacing it?

No, this creates a file config/config.txt into the runtime directory.

If you don’t want to write the config file to a hidden/hard to find/unknown to the user directory then you could read/write it from the same dir that the p3d has placed

from direct.showbase.AppRunnerGlobal import appRunner
if appRunner: 
    CurrentDirectory=appRunner.p3dFilename.getDirname()

Wezu’s suggestion works great for me. Thanks everyone for your help in understanding this!

Just for the record, you don’t use the same path for writing as you do for reading. When you read, you’re reading from the file in the mounted multifile, and when you write, you’re writing to disk. So the “config” directory may not actually exist on disk even though it’s in the multifile.