class_directory.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the Directory.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_Directory:
  5. Directory
  6. =========
  7. **Inherits:** :ref:`Reference<class_reference>` **<** :ref:`Object<class_object>`
  8. **Category:** Core
  9. Brief Description
  10. -----------------
  11. Type used to handle the filesystem.
  12. Member Functions
  13. ----------------
  14. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. | :ref:`int<class_int>` | :ref:`change_dir<class_Directory_change_dir>` **(** :ref:`String<class_string>` todir **)** |
  16. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | :ref:`int<class_int>` | :ref:`copy<class_Directory_copy>` **(** :ref:`String<class_string>` from, :ref:`String<class_string>` to **)** |
  18. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | :ref:`bool<class_bool>` | :ref:`current_is_dir<class_Directory_current_is_dir>` **(** **)** const |
  20. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  21. | :ref:`bool<class_bool>` | :ref:`dir_exists<class_Directory_dir_exists>` **(** :ref:`String<class_string>` path **)** |
  22. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | :ref:`bool<class_bool>` | :ref:`file_exists<class_Directory_file_exists>` **(** :ref:`String<class_string>` path **)** |
  24. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | :ref:`String<class_string>` | :ref:`get_current_dir<class_Directory_get_current_dir>` **(** **)** |
  26. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`int<class_int>` | :ref:`get_current_drive<class_Directory_get_current_drive>` **(** **)** |
  28. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | :ref:`String<class_string>` | :ref:`get_drive<class_Directory_get_drive>` **(** :ref:`int<class_int>` idx **)** |
  30. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`int<class_int>` | :ref:`get_drive_count<class_Directory_get_drive_count>` **(** **)** |
  32. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`String<class_string>` | :ref:`get_next<class_Directory_get_next>` **(** **)** |
  34. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`int<class_int>` | :ref:`get_space_left<class_Directory_get_space_left>` **(** **)** |
  36. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`int<class_int>` | :ref:`list_dir_begin<class_Directory_list_dir_begin>` **(** :ref:`bool<class_bool>` skip_navigational=false, :ref:`bool<class_bool>` skip_hidden=false **)** |
  38. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | void | :ref:`list_dir_end<class_Directory_list_dir_end>` **(** **)** |
  40. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`int<class_int>` | :ref:`make_dir<class_Directory_make_dir>` **(** :ref:`String<class_string>` path **)** |
  42. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`int<class_int>` | :ref:`make_dir_recursive<class_Directory_make_dir_recursive>` **(** :ref:`String<class_string>` path **)** |
  44. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`int<class_int>` | :ref:`open<class_Directory_open>` **(** :ref:`String<class_string>` path **)** |
  46. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`int<class_int>` | :ref:`remove<class_Directory_remove>` **(** :ref:`String<class_string>` path **)** |
  48. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`int<class_int>` | :ref:`rename<class_Directory_rename>` **(** :ref:`String<class_string>` from, :ref:`String<class_string>` to **)** |
  50. +------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. Description
  52. -----------
  53. Directory type. It is used to manage directories and their content (not restricted to the project folder).
  54. Here is an example on how to iterate through the files of a directory:
  55. ::
  56. func dir_contents(path):
  57. var dir = Directory.new()
  58. if dir.open(path) == OK:
  59. dir.list_dir_begin()
  60. var file_name = dir.get_next()
  61. while (file_name != ""):
  62. if dir.current_is_dir():
  63. print("Found directory: " + file_name)
  64. else:
  65. print("Found file: " + file_name)
  66. file_name = dir.get_next()
  67. else:
  68. print("An error occurred when trying to access the path.")
  69. Member Function Description
  70. ---------------------------
  71. .. _class_Directory_change_dir:
  72. - :ref:`int<class_int>` **change_dir** **(** :ref:`String<class_string>` todir **)**
  73. Change 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``).
  74. The method returns one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK or ERR\_\*).
  75. .. _class_Directory_copy:
  76. - :ref:`int<class_int>` **copy** **(** :ref:`String<class_string>` from, :ref:`String<class_string>` to **)**
  77. Copy 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.
  78. Returns one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK, FAILED or ERR\_\*).
  79. .. _class_Directory_current_is_dir:
  80. - :ref:`bool<class_bool>` **current_is_dir** **(** **)** const
  81. Return whether the current item processed with the last :ref:`get_next<class_Directory_get_next>` call is a directory (``.`` and ``..`` are considered directories).
  82. .. _class_Directory_dir_exists:
  83. - :ref:`bool<class_bool>` **dir_exists** **(** :ref:`String<class_string>` path **)**
  84. Return whether the target directory exists. The argument can be relative to the current directory, or an absolute path.
  85. .. _class_Directory_file_exists:
  86. - :ref:`bool<class_bool>` **file_exists** **(** :ref:`String<class_string>` path **)**
  87. Return whether the target file exists. The argument can be relative to the current directory, or an absolute path.
  88. .. _class_Directory_get_current_dir:
  89. - :ref:`String<class_string>` **get_current_dir** **(** **)**
  90. Return the absolute path to the currently opened directory (e.g. ``res://folder`` or ``C:\tmp\folder``).
  91. .. _class_Directory_get_current_drive:
  92. - :ref:`int<class_int>` **get_current_drive** **(** **)**
  93. Returns the currently opened directory's drive index. See :ref:`get_drive<class_Directory_get_drive>` to convert returned index to the name of the drive.
  94. .. _class_Directory_get_drive:
  95. - :ref:`String<class_string>` **get_drive** **(** :ref:`int<class_int>` idx **)**
  96. On Windows, return the name of the drive (partition) passed as an argument (e.g. ``C:``). On other platforms, or if the requested drive does not existed, the method returns an empty String.
  97. .. _class_Directory_get_drive_count:
  98. - :ref:`int<class_int>` **get_drive_count** **(** **)**
  99. On Windows, return the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0.
  100. .. _class_Directory_get_next:
  101. - :ref:`String<class_string>` **get_next** **(** **)**
  102. Return the next element (file or directory) in the current directory (including ``.`` and ``..``, unless ``skip_navigational`` was given to :ref:`list_dir_begin<class_Directory_list_dir_begin>`).
  103. 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_list_dir_end>` would not be mandatory in such a case).
  104. .. _class_Directory_get_space_left:
  105. - :ref:`int<class_int>` **get_space_left** **(** **)**
  106. On Unix desktop systems, return the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1.
  107. .. _class_Directory_list_dir_begin:
  108. - :ref:`int<class_int>` **list_dir_begin** **(** :ref:`bool<class_bool>` skip_navigational=false, :ref:`bool<class_bool>` skip_hidden=false **)**
  109. Initialise the stream used to list all files and directories using the :ref:`get_next<class_Directory_get_next>` function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with :ref:`list_dir_end<class_Directory_list_dir_end>`.
  110. If you pass ``skip_navigational``, then ``.`` and ``..`` would be filtered out.
  111. If you pass ``skip_hidden``, then hidden files would be filtered out.
  112. .. _class_Directory_list_dir_end:
  113. - void **list_dir_end** **(** **)**
  114. Close the current stream opened with :ref:`list_dir_begin<class_Directory_list_dir_begin>` (whether it has been fully processed with :ref:`get_next<class_Directory_get_next>` or not does not matter).
  115. .. _class_Directory_make_dir:
  116. - :ref:`int<class_int>` **make_dir** **(** :ref:`String<class_string>` path **)**
  117. Create 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_make_dir_recursive>`).
  118. The method returns one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK, FAILED or ERR\_\*).
  119. .. _class_Directory_make_dir_recursive:
  120. - :ref:`int<class_int>` **make_dir_recursive** **(** :ref:`String<class_string>` path **)**
  121. Create a target directory and all necessary intermediate directories in its path, by calling :ref:`make_dir<class_Directory_make_dir>` recursively. The argument can be relative to the current directory, or an absolute path.
  122. Return one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK, FAILED or ERR\_\*).
  123. .. _class_Directory_open:
  124. - :ref:`int<class_int>` **open** **(** :ref:`String<class_string>` path **)**
  125. Open 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``).
  126. The method returns one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK or ERR\_\*).
  127. .. _class_Directory_remove:
  128. - :ref:`int<class_int>` **remove** **(** :ref:`String<class_string>` path **)**
  129. Delete 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.
  130. Return one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK or FAILED).
  131. .. _class_Directory_rename:
  132. - :ref:`int<class_int>` **rename** **(** :ref:`String<class_string>` from, :ref:`String<class_string>` to **)**
  133. Rename (move) 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.
  134. Return one of the error code constants defined in :ref:`@GlobalScope<class_@globalscope>` (OK or FAILED).