+6 votes

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

in Engine by (466 points)

2 Answers

+10 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

by (1,269 points)
selected by
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")

+3 votes

to check whether a directory exists or not, use

var directory = Directory.new();
var doFileExists = directory.dir_exists(PATH_2_DIRECTORY)
by (44 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.
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 webmaster@godotengine.org with your username.

Categories