Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
DirAccess¶
Inherits: RefCounted < Object
Provides methods for managing directories and their content.
Description¶
This class is used to manage directories and their content, even outside of the project folder.
DirAccess can't be instantiated directly. Instead it is created with a static method that takes a path for which it will be opened.
Most of the methods have a static alternative that can be used without creating a DirAccess. Static methods only support absolute paths (including res://
and user://
).
# Standard
var dir = DirAccess.open("user://levels")
dir.make_dir("world1")
# Static
DirAccess.make_dir_absolute("user://levels/world1")
Note: Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources.
Here is an example on how to iterate through the files of a directory:
func dir_contents(path):
var dir = DirAccess.open(path)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
print("Found directory: " + file_name)
else:
print("Found file: " + file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
public void DirContents(string path)
{
using var dir = DirAccess.Open(path);
if (dir != null)
{
dir.ListDirBegin();
string fileName = dir.GetNext();
while (fileName != "")
{
if (dir.CurrentIsDir())
{
GD.Print($"Found directory: {fileName}");
}
else
{
GD.Print($"Found file: {fileName}");
}
fileName = dir.GetNext();
}
}
else
{
GD.Print("An error occurred when trying to access the path.");
}
}
Tutorials¶
Properties¶
Methods¶
change_dir ( String to_dir ) |
|
copy_absolute ( String from, String to, int chmod_flags=-1 ) static |
|
current_is_dir ( ) const |
|
dir_exists ( String path ) |
|
dir_exists_absolute ( String path ) static |
|
file_exists ( String path ) |