0 votes

Python comes with a "a" (Append) flag to append text data to an existing txt/json file(refer).

However, in gdscript such an option is not provided (refer).

Any workarounds ?

in Engine by (51 points)

1 Answer

0 votes
Best answer

I would make the following suggestion:

var file = File.new()
var path = "user://example.txt"

if file.file_exists(path):
    file.open(path, File.READ_WRITE)
    file.seek_end()
else:
    file.open(path, File.WRITE)

# append lines to file
file.store_line("example")

file.close()

You can obviously shorten the code when you are sure that the file already exists.
The key method here is seek_end()

by (58 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.