Error trying to open a file. What am I doing wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By korayozbay

Hi,
I am trying to implement save/load data from a file following a tutorial (https://www.youtube.com/watch?v=2U3iiqd4Wjg&list=PL4vbr3u7UKWqwQlvwvgNcgDL1p_3hcNn2&index=56) but it fails. When I print the error, it just says 7. How can I know what error 7 means? This is the code:

onready var path = "user://save.dat"

func load_data():
var file = File.new()
var err = file.open(path, File.READ)
if err != OK:
	print("error opening data file")
	print(str(err))
	return default_level_info		
var read = {}
read = file.get_var()
return read 
:bust_in_silhouette: Reply From: Calinou

To know what the error code means, check the Error enum documentation. 7 is for ERR_FILE_NOT_FOUND. To read a file, it needs to exist already. To create the file if it doesn’t exist (and truncate it if it exists), use the File.WRITE mode instead of File.READ.

Call file_exists(path) from your File instance to check whether a file exists at the given path.

Thank you so much, it worked exactly as you described.

korayozbay | 2020-05-05 13:19