class_directory.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/Directory.xml.
  6. .. _class_Directory:
  7. Directory
  8. =========
  9. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. Type used to handle the filesystem.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. Directory type. It is used to manage directories and their content (not restricted to the project folder).
  15. When creating a new **Directory**, its default opened directory will be ``res://``. This may change in the future, so it is advised to always use :ref:`open<class_Directory_method_open>` to initialize your **Directory** where you want to operate, with explicit error checking.
  16. \ **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 :ref:`ResourceLoader<class_ResourceLoader>` to access imported resources.
  17. Here is an example on how to iterate through the files of a directory:
  18. ::
  19. func dir_contents(path):
  20. var dir = Directory.new()
  21. if dir.open(path) == OK:
  22. dir.list_dir_begin()
  23. var file_name = dir.get_next()
  24. while file_name != "":
  25. if dir.current_is_dir():
  26. print("Found directory: " + file_name)
  27. else:
  28. print("Found file: " + file_name)
  29. file_name = dir.get_next()
  30. else:
  31. print("An error occurred when trying to access the path.")
  32. .. rst-class:: classref-introduction-group
  33. Tutorials
  34. ---------
  35. - :doc:`../tutorials/scripting/filesystem`
  36. .. rst-class:: classref-reftable-group
  37. Methods
  38. -------
  39. .. table::
  40. :widths: auto
  41. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`change_dir<class_Directory_method_change_dir>` **(** :ref:`String<class_String>` todir **)** |
  43. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`copy<class_Directory_method_copy>` **(** :ref:`String<class_String>` from, :ref:`String<class_String>` to **)** |
  45. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`bool<class_bool>` | :ref:`current_is_dir<class_Directory_method_current_is_dir>` **(** **)** |const| |
  47. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`bool<class_bool>` | :ref:`dir_exists<class_Directory_method_dir_exists>` **(** :ref:`String<class_String>` path **)** |
  49. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`bool<class_bool>` | :ref:`file_exists<class_Directory_method_file_exists>` **(** :ref:`String<class_String>` path **)** |
  51. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`String<class_String>` | :ref:`get_current_dir<class_Directory_method_get_current_dir>` **(** **)** |
  53. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`int<class_int>` | :ref:`get_current_drive<class_Directory_method_get_current_drive>` **(** **)** |
  55. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`String<class_String>` | :ref:`get_drive<class_Directory_method_get_drive>` **(** :ref:`int<class_int>` idx **)** |
  57. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`int<class_int>` | :ref:`get_drive_count<class_Directory_method_get_drive_count>` **(** **)** |
  59. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`String<class_String>` | :ref:`get_next<class_Directory_method_get_next>` **(** **)** |
  61. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`int<class_int>` | :ref:`get_space_left<class_Directory_method_get_space_left>` **(** **)** |
  63. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`list_dir_begin<class_Directory_method_list_dir_begin>` **(** :ref:`bool<class_bool>` skip_navigational=false, :ref:`bool<class_bool>` skip_hidden=false **)** |
  65. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | void | :ref:`list_dir_end<class_Directory_method_list_dir_end>` **(** **)** |
  67. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`make_dir<class_Directory_method_make_dir>` **(** :ref:`String<class_String>` path **)** |
  69. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`make_dir_recursive<class_Directory_method_make_dir_recursive>` **(** :ref:`String<class_String>` path **)** |
  71. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`open<class_Directory_method_open>` **(** :ref:`String<class_String>` path **)** |
  73. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`remove<class_Directory_method_remove>` **(** :ref:`String<class_String>` path **)** |
  75. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`rename<class_Directory_method_rename>` **(** :ref:`String<class_String>` from, :ref:`String<class_String>` to **)** |
  77. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. .. rst-class:: classref-section-separator
  79. ----
  80. .. rst-class:: classref-descriptions-group
  81. Method Descriptions
  82. -------------------
  83. .. _class_Directory_method_change_dir:
  84. .. rst-class:: classref-method
  85. :ref:`Error<enum_@GlobalScope_Error>` **change_dir** **(** :ref:`String<class_String>` todir **)**
  86. 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``).
  87. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  88. .. rst-class:: classref-item-separator
  89. ----
  90. .. _class_Directory_method_copy:
  91. .. rst-class:: classref-method
  92. :ref:`Error<enum_@GlobalScope_Error>` **copy** **(** :ref:`String<class_String>` from, :ref:`String<class_String>` to **)**
  93. 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.
  94. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  95. .. rst-class:: classref-item-separator
  96. ----
  97. .. _class_Directory_method_current_is_dir:
  98. .. rst-class:: classref-method
  99. :ref:`bool<class_bool>` **current_is_dir** **(** **)** |const|
  100. Returns whether the current item processed with the last :ref:`get_next<class_Directory_method_get_next>` call is a directory (``.`` and ``..`` are considered directories).
  101. .. rst-class:: classref-item-separator
  102. ----
  103. .. _class_Directory_method_dir_exists:
  104. .. rst-class:: classref-method
  105. :ref:`bool<class_bool>` **dir_exists** **(** :ref:`String<class_String>` path **)**
  106. Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.
  107. .. rst-class:: classref-item-separator
  108. ----
  109. .. _class_Directory_method_file_exists:
  110. .. rst-class:: classref-method
  111. :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_String>` path **)**
  112. Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.
  113. .. rst-class:: classref-item-separator
  114. ----
  115. .. _class_Directory_method_get_current_dir:
  116. .. rst-class:: classref-method
  117. :ref:`String<class_String>` **get_current_dir** **(** **)**
  118. Returns the absolute path to the currently opened directory (e.g. ``res://folder`` or ``C:\tmp\folder``).
  119. .. rst-class:: classref-item-separator
  120. ----
  121. .. _class_Directory_method_get_current_drive:
  122. .. rst-class:: classref-method
  123. :ref:`int<class_int>` **get_current_drive** **(** **)**
  124. Returns the currently opened directory's drive index. See :ref:`get_drive<class_Directory_method_get_drive>` to convert returned index to the name of the drive.
  125. .. rst-class:: classref-item-separator
  126. ----
  127. .. _class_Directory_method_get_drive:
  128. .. rst-class:: classref-method
  129. :ref:`String<class_String>` **get_drive** **(** :ref:`int<class_int>` idx **)**
  130. On Windows, returns the name of the drive (partition) passed as an argument (e.g. ``C:``).
  131. On macOS, returns the path to the mounted volume passed as an argument.
  132. On Linux, returns the path to the mounted volume or GTK 3 bookmark passed as an argument.
  133. On other platforms, or if the requested drive does not exist, the method returns an empty String.
  134. .. rst-class:: classref-item-separator
  135. ----
  136. .. _class_Directory_method_get_drive_count:
  137. .. rst-class:: classref-method
  138. :ref:`int<class_int>` **get_drive_count** **(** **)**
  139. On Windows, returns the number of drives (partitions) mounted on the current filesystem.
  140. On macOS, returns the number of mounted volumes.
  141. On Linux, returns the number of mounted volumes and GTK 3 bookmarks.
  142. On other platforms, the method returns 0.
  143. .. rst-class:: classref-item-separator
  144. ----
  145. .. _class_Directory_method_get_next:
  146. .. rst-class:: classref-method
  147. :ref:`String<class_String>` **get_next** **(** **)**
  148. Returns the next element (file or directory) in the current directory (including ``.`` and ``..``, unless ``skip_navigational`` was given to :ref:`list_dir_begin<class_Directory_method_list_dir_begin>`).
  149. 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. :ref:`list_dir_end<class_Directory_method_list_dir_end>` would not be mandatory in such a case).
  150. .. rst-class:: classref-item-separator
  151. ----
  152. .. _class_Directory_method_get_space_left:
  153. .. rst-class:: classref-method
  154. :ref:`int<class_int>` **get_space_left** **(** **)**
  155. On UNIX desktop systems, returns the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1.
  156. .. rst-class:: classref-item-separator
  157. ----
  158. .. _class_Directory_method_list_dir_begin:
  159. .. rst-class:: classref-method
  160. :ref:`Error<enum_@GlobalScope_Error>` **list_dir_begin** **(** :ref:`bool<class_bool>` skip_navigational=false, :ref:`bool<class_bool>` skip_hidden=false **)**
  161. Initializes the stream used to list all files and directories using the :ref:`get_next<class_Directory_method_get_next>` function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with :ref:`list_dir_end<class_Directory_method_list_dir_end>`.
  162. If ``skip_navigational`` is ``true``, ``.`` and ``..`` are filtered out.
  163. If ``skip_hidden`` is ``true``, hidden files are filtered out.
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_Directory_method_list_dir_end:
  167. .. rst-class:: classref-method
  168. void **list_dir_end** **(** **)**
  169. Closes the current stream opened with :ref:`list_dir_begin<class_Directory_method_list_dir_begin>` (whether it has been fully processed with :ref:`get_next<class_Directory_method_get_next>` does not matter).
  170. .. rst-class:: classref-item-separator
  171. ----
  172. .. _class_Directory_method_make_dir:
  173. .. rst-class:: classref-method
  174. :ref:`Error<enum_@GlobalScope_Error>` **make_dir** **(** :ref:`String<class_String>` path **)**
  175. 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 :ref:`make_dir_recursive<class_Directory_method_make_dir_recursive>`).
  176. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  177. .. rst-class:: classref-item-separator
  178. ----
  179. .. _class_Directory_method_make_dir_recursive:
  180. .. rst-class:: classref-method
  181. :ref:`Error<enum_@GlobalScope_Error>` **make_dir_recursive** **(** :ref:`String<class_String>` path **)**
  182. Creates a target directory and all necessary intermediate directories in its path, by calling :ref:`make_dir<class_Directory_method_make_dir>` recursively. The argument can be relative to the current directory, or an absolute path.
  183. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  184. .. rst-class:: classref-item-separator
  185. ----
  186. .. _class_Directory_method_open:
  187. .. rst-class:: classref-method
  188. :ref:`Error<enum_@GlobalScope_Error>` **open** **(** :ref:`String<class_String>` path **)**
  189. 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``).
  190. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  191. .. rst-class:: classref-item-separator
  192. ----
  193. .. _class_Directory_method_remove:
  194. .. rst-class:: classref-method
  195. :ref:`Error<enum_@GlobalScope_Error>` **remove** **(** :ref:`String<class_String>` path **)**
  196. 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.
  197. If you don't want to delete the file/directory permanently, use :ref:`OS.move_to_trash<class_OS_method_move_to_trash>` instead.
  198. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  199. .. rst-class:: classref-item-separator
  200. ----
  201. .. _class_Directory_method_rename:
  202. .. rst-class:: classref-method
  203. :ref:`Error<enum_@GlobalScope_Error>` **rename** **(** :ref:`String<class_String>` from, :ref:`String<class_String>` to **)**
  204. 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.
  205. Returns one of the :ref:`Error<enum_@GlobalScope_Error>` code constants (``OK`` on success).
  206. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  207. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  208. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  209. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`