+3 votes

Is there a simple (universal) way to check if a file exists?

asked Feb 27, 2016 in Engine by Tybobobo (452 points)

2 Answers

+8 votes
Best answer
var file2Check = File.new()
var doFileExists = file2Check.file_exists(PATH_2_FILE):

You can also use Directory class:

var directory = Directory.new();
var doFileExists = directory.file_exists(PATH_2_FILE)

PATH_2_FILE should be in "res://data/file.extension" format

answered Feb 27, 2016 by kubecz3k (1,253 points)
selected Feb 27, 2016 by Tybobobo
Perfect! Thank you :)
I was using ConfigFile - but that does not seem to have anything similar (yet)
Note that you can make it a one-liner with `Directory.new().file_exists(PATH_2_FILE)` and `File.new().file_exists(PATH_2_FILE)`

For resources, use ResourceLoader.exists("res://sprite.tscn")

+1 vote

to check whether a directory exists or not, use

var directory = Directory.new();
var doFileExists = directory.dir_exists(PATH_2_DIRECTORY)
answered Mar 6, 2019 by salihkallai (40 points)

note: GODOT 3.0+

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 How to use this Q&A? before posting your first questions.