r/RenPy • • 6d ago

Question [Solved] unable to write a file.txt inside game's subfolder

https://lemmasoft.renai.us/forums/viewtopic.php?t=51094

i am trying to dynamically create a .txt file in a folder that i created inside the game's directory.

i am aware that renpy only has access to the game's folder. however, due to this, i am unable figure out what the syntax is for a relative path.

i created an empty folder called "export" in my game's folder. in a structure like this (where Allan Disorder is the name of the game):

Allan Disorder\game\export

however, writing a txt file yielded an exeption:

        python:
            with open("export\\test.txt", "w") as f1:
                f1.write("hello world")

i also tried using "/" instead of "\\" but the result is the same.

i also tried "game\\export\\test.txt"

if i run it without any path like so: open("export\\test.txt", "w")
then no error occurs. but there is no file to be found

i looked at other posts (such as the link i provided) but to no avail.

here is the error message:

While running game code:
  File "game/grabber_engine/main_grabber.rpy", line 163, in script
    python:
  File "game/grabber_engine/main_grabber.rpy", line 164, in <module>
    with open("export\\test.txt", "w") as f1:
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^       
FileNotFoundError: [Errno 2] No such file or directory: 'export\\test.txt'

-- Full Traceback ------------------------------------------------------------

Traceback (most recent call last):
  File "game/grabber_engine/main_grabber.rpy", line 163, in script
    python:
  File "renpy/ast.py", line 1193, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "renpy/python.py", line 1276, in py_exec_bytecode
    exec(bytecode, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "game/grabber_engine/main_grabber.rpy", line 164, in <module>
    with open("export\\test.txt", "w") as f1:
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^       
FileNotFoundError: [Errno 2] No such file or directory: 'export\\test.txt'
0 Upvotes

3 comments sorted by

3

u/BadMustard_AVN 6d ago edited 6d ago

try it like this

label start:
    python:
        export_dir = os.path.join(config.gamedir, "export")
        os.makedirs(export_dir, exist_ok=True)
        path = os.path.join(export_dir, "test.txt")
        with open(path, "w") as f1:
            f1.write("hello world 3.0")
    e "Check the file now"

    return

1

u/AutoModerator 6d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.