Index · Object → RefCounted → DirAccess

DirAccess

Provides methods for managing directories and their content.

Class 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: Accessing project ("res://") directories once exported may behave unexpectedly as some files are converted to engine-specific formats and their original source files may not be present in the expected PCK package. Because of this, to access resources in an exported project, it is recommended to use ResourceLoader instead of FileAccess.

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.")

Keep in mind that file names may change or be remapped after export. If you want to see the actual resource file list as it appears in the editor, use ResourceLoader.list_directory() instead.

Properties

var include_hidden: bool

If true, hidden files are included when navigating the directory.

Affects list_dir_begin(), get_directories() and get_files().

var include_navigational: bool

If true, . and .. are included when navigating the directory.

Affects list_dir_begin() and get_directories().

Methods

func change_dir(to_dir: String) -> Error

Changes the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. newdir or ../newdir), or an absolute path (e.g. /tmp/newdir or res://somedir/newdir).

Returns one of the Error code constants (OK on success).

Note: The new directory must be within the same scope, e.g. when you had opened a directory inside res://, you can't change it to user:// directory. If you need to open a directory in another access scope, use open() to create a new instance instead.

func copy(from: String, to: String, chmod_flags: int = -1) -> Error

Copies the from file to the to destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten.

If chmod_flags is different than -1, the Unix permissions for the destination path will be set to the provided value, if available on the current operating system.

Returns one of the Error code constants (OK on success).

func copy_absolute(from: String, to: String, chmod_flags: int = -1) -> Error

Static version of copy(). Supports only absolute paths.

func create_temp(prefix: String = "", keep: bool = false) -> DirAccess

Creates a temporary directory. This directory will be freed when the returned DirAccess is freed.

If prefix is not empty, it will be prefixed to the directory name, separated by a -.

If keep is true, the directory is not deleted when the returned DirAccess is freed.

Returns null if opening the directory failed. You can use get_open_error() to check the error that occurred.

func current_is_dir() -> bool

Returns whether the current item processed with the last get_next() call is a directory (. and .. are considered directories).

func dir_exists(path: String) -> bool

Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.

Note: The returned bool in the editor and after exporting when used on a path in the res:// directory may be different. Some files are converted to engine-specific formats when exported, potentially changing the directory structure.

func dir_exists_absolute(path: String) -> bool

Static version of dir_exists(). Supports only absolute paths.

Note: The returned bool in the editor and after exporting when used on a path in the res:// directory may be different. Some files are converted to engine-specific formats when exported, potentially changing the directory structure.

func file_exists(path: String) -> bool

Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.

For a static equivalent, use FileAccess.file_exists().

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. See ResourceLoader.exists() for an alternative approach that takes resource remapping into account.

func get_current_dir(include_drive: bool = true) -> String

Returns the absolute path to the currently opened directory (e.g. res://folder or C:\tmp\folder).

func get_current_drive() -> int

Returns the currently opened directory's drive index. See get_drive_name() to convert returned index to the name of the drive.

func get_directories() -> PackedStringArray

Returns a PackedStringArray containing filenames of the directory contents, excluding files. The array is sorted alphabetically.

Affected by include_hidden and include_navigational.

Note: The returned directories in the editor and after exporting in the res:// directory may differ as some files are converted to engine-specific formats when exported.

func get_directories_at(path: String) -> PackedStringArray

Returns a PackedStringArray containing filenames of the directory contents, excluding files, at the given path. The array is sorted alphabetically.

Use get_directories() if you want more control of what gets included.

Note: The returned directories in the editor and after exporting in the res:// directory may differ as some files are converted to engine-specific formats when exported.

func get_drive_count() -> int

On Windows, returns the number of drives (partitions) mounted on the current filesystem.

On macOS and Android, returns the number of mounted volumes.

On Linux, returns the number of mounted volumes and GTK 3 bookmarks.

On other platforms, the method returns 0.

func get_drive_label(idx: int) -> String

On Windows, returns the label of the drive (partition) passed as an argument.

On other platforms, or if the requested drive does not exist, returns an empty String.

func get_drive_name(idx: int) -> String

On Windows, returns the name of the drive (partition) passed as an argument (e.g. C:).

On macOS, returns the path to the mounted volume passed as an argument.

On Linux, returns the path to the mounted volume or GTK 3 bookmark passed as an argument.

On Android (API level 30+), returns the path to the mounted volume as an argument.

On other platforms, or if the requested drive does not exist, returns an empty String.

func get_files() -> PackedStringArray

Returns a PackedStringArray containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.

Affected by include_hidden.

Note: When used on a res:// path in an exported project, only the files actually included in the PCK at the given folder level are returned. In practice, this means that since imported resources are stored in a top-level .godot/ folder, only paths to *.gd and *.import files are returned (plus a few files such as project.godot or project.binary and the project icon). In an exported project, the list of returned files will also vary depending on whether ProjectSettings.editor/export/convert_text_resources_to_binary is true.

func get_files_at(path: String) -> PackedStringArray

Returns a PackedStringArray containing filenames of the directory contents, excluding directories, at the given path. The array is sorted alphabetically.

Use get_files() if you want more control of what gets included.

Note: When used on a res:// path in an exported project, only the files included in the PCK at the given folder level are returned. In practice, this means that since imported resources are stored in a top-level .godot/ folder, only paths to .gd and .import files are returned (plus a few other files, such as project.godot or project.binary and the project icon). In an exported project, the list of returned files will also vary depending on ProjectSettings.editor/export/convert_text_resources_to_binary.

func get_filesystem_type() -> String

Returns file system type name of the current directory's disk. Returned values are uppercase strings like NTFS, FAT32, EXFAT, APFS, EXT4, BTRFS, and so on.

Note: This method is implemented on macOS, Linux, Windows and for PCK virtual file system.

func get_next() -> String

Returns the next element (file or directory) in the current directory.

The name of the file or directory is returned (and not its full path). Once the stream has been fully processed, the method returns an empty String and closes the stream automatically (i.e. list_dir_end() would not be mandatory in such a case).

func get_open_error() -> Error

Returns the result of the last open() call in the current thread.

func get_space_left() -> int

Returns the available space on the current directory's disk, in bytes. Returns 0 if the platform-specific method to query the available space fails.

func is_bundle(path: String) -> bool

Returns true if the directory is a macOS bundle.

Note: This method is implemented on macOS.

func is_case_sensitive(path: String) -> bool

Returns true if the file system or directory use case sensitive file names.

Note: This method is implemented on macOS, Linux (for EXT4 and F2FS filesystems only) and Windows. On other platforms, it always returns true.

func is_equivalent(path_a: String, path_b: String) -> bool

Returns true if paths path_a and path_b resolve to the same file system object. Returns false otherwise, even if the files are bit-for-bit identical (e.g., identical copies of the file that are not symbolic links).

func list_dir_begin() -> Error

Initializes the stream used to list all files and directories using the get_next() function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with list_dir_end().

Affected by include_hidden and include_navigational.

Note: The order of files and directories returned by this method is not deterministic, and can vary between operating systems. If you want a list of all files or folders sorted alphabetically, use get_files() or get_directories().

func list_dir_end() -> void

Closes the current stream opened with list_dir_begin() (whether it has been fully processed with get_next() does not matter).

func make_dir(path: String) -> Error

Creates a directory. The argument can be relative to the current directory, or an absolute path. The target directory should be placed in an already existing directory (to create the full path recursively, see make_dir_recursive()).

Returns one of the Error code constants (OK on success).

func make_dir_absolute(path: String) -> Error

Static version of make_dir(). Supports only absolute paths.

func make_dir_recursive(path: String) -> Error

Creates a target directory and all necessary intermediate directories in its path, by calling make_dir() recursively. The argument can be relative to the current directory, or an absolute path.

Returns one of the Error code constants (OK on success).

func make_dir_recursive_absolute(path: String) -> Error

Static version of make_dir_recursive(). Supports only absolute paths.

func open(path: String) -> DirAccess

Creates a new DirAccess object and opens an existing directory of the filesystem. The path argument can be within the project tree (res://folder), the user directory (user://folder) or an absolute path of the user filesystem (e.g. /tmp/folder or C:\tmp\folder).

Returns null if opening the directory failed. You can use get_open_error() to check the error that occurred.

func remove(path: String) -> Error

Permanently deletes the target file or an empty directory. The argument can be relative to the current directory, or an absolute path. If the target directory is not empty, the operation will fail.

If you don't want to delete the file/directory permanently, use OS.move_to_trash() instead.

Returns one of the Error code constants (OK on success).

func remove_absolute(path: String) -> Error

Static version of remove(). Supports only absolute paths.

func rename(from: String, to: String) -> Error

Renames (move) the from file or directory to the to destination. Both arguments should be paths to files or directories, either relative or absolute. If the destination file or directory exists and is not access-protected, it will be overwritten.

Returns one of the Error code constants (OK on success).

func rename_absolute(from: String, to: String) -> Error

Static version of rename(). Supports only absolute paths.

Inherited from RefCounted (4)
func init_ref() -> bool
func reference() -> bool
func unreference() -> bool
Inherited from Object (62)
func _get(property: StringName) -> Variant
func _get_property_list() -> Dictionary[]
func _init() -> void
func _iter_get(iter: Variant) -> Variant
func _iter_init(iter: Array) -> bool
func _iter_next(iter: Array) -> bool
func _notification(what: int) -> void
func _set(property: StringName, value: Variant) -> bool
func _to_string() -> String
func _validate_property(property: Dictionary) -> void
func add_user_signal(signal: String, arguments: Array = []) -> void
func call(method: StringName) -> Variant
func callv(method: StringName, arg_array: Array) -> Variant
func cancel_free() -> void
func connect(signal: StringName, callable: Callable, flags: int = 0) -> Error
func disconnect(signal: StringName, callable: Callable) -> void
func emit_signal(signal: StringName) -> Error
func free() -> void
func get(property: StringName) -> Variant
func get_class() -> String
func get_incoming_connections() -> Dictionary[]
func get_indexed(property_path: NodePath) -> Variant
func get_meta(name: StringName, default: Variant = null) -> Variant
func get_meta_list() -> StringName[]
func get_method_list() -> Dictionary[]
func get_property_list() -> Dictionary[]
func get_signal_connection_list(signal: StringName) -> Dictionary[]
func get_signal_list() -> Dictionary[]
func has_meta(name: StringName) -> bool
func has_method(method: StringName) -> bool
func has_signal(signal: StringName) -> bool
func is_class(class: StringName) -> bool
func is_connected(signal: StringName, callable: Callable) -> bool
func notification(what: int, reversed: bool = false) -> void
func remove_meta(name: StringName) -> void
func remove_user_signal(signal: StringName) -> void
func set(property: StringName, value: Variant) -> void
func set_block_signals(enable: bool) -> void
func set_deferred(property: StringName, value: Variant) -> void
func set_indexed(property_path: NodePath, value: Variant) -> void
func set_message_translation(enable: bool) -> void
func set_meta(name: StringName, value: Variant) -> void
func set_script(script: Variant) -> void
func set_translation_domain(domain: StringName) -> void
func to_string() -> String
func tr(message: StringName, context: StringName = &"") -> String
func tr_n(message: StringName, plural_message: StringName, n: int, context: StringName = &"") -> String

Signals

Inherited from Object (2)