号码簿¶
类别: 核心
简要说明¶
用于处理文件系统的类型。
方法¶
change_dir ( String todir ) |
|
current_is_dir ( ) const |
|
dir_exists ( String path ) |
|
file_exists ( String path ) |
|
get_current_dir ( ) |
|
get_drive_count ( ) |
|
get_next ( ) |
|
get_space_left ( ) |
|
list_dir_begin ( bool skip_navigational=false, bool skip_hidden=false ) |
|
无效 |
list_dir_end ( ) |
make_dir_recursive ( String path ) |
|
描述¶
目录类型。它用于管理目录及其内容(不限于项目文件夹)。
下面是一个如何遍历目录文件的示例:
func dir_contents(path):
var dir = Directory.new()
if dir.open(path) == OK:
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.")
方法说明¶
将当前打开的目录更改为作为参数传递的目录。参数可以相对于当前目录(例如 newdir
或 ../newdir
或绝对路径(例如 /tmp/newdir
或 res://somedir/newdir
)
The method returns one of the error code constants defined in @GlobalScope (OK
or ERR_*
).
复制 from
文件发送至 to
目的地。两个参数都应该是指向文件的路径,无论是相对的还是绝对的。如果目标文件存在且不受访问保护,它将被覆盖。
Returns one of the error code constants defined in @GlobalScope (OK
, FAILED
or ERR_*
).
bool current_is_dir ( ) const
返回当前项是否使用最后一个 get_next 呼叫是一个目录 (.
and ..
被视为目录)。
返回目标目录是否存在。参数可以是相对于当前目录或绝对路径的。
返回目标文件是否存在。参数可以是相对于当前目录或绝对路径的。
String get_current_dir ( )
返回当前打开目录的绝对路径(例如 res://folder
或 C:\tmp\folder
)
int get_current_drive ( )
返回当前打开的目录的驱动器索引。见 get_drive 将返回的索引转换为驱动器的名称。
在Windows上,返回作为参数传递的驱动器(分区)的名称(例如 C:
)在其他平台上,或者如果请求的驱动器不存在,则该方法返回空字符串。
int get_drive_count ( )
在Windows上,返回当前文件系统上安装的驱动器(分区)数。在其他平台上,该方法返回0。
String get_next ( )
返回当前目录中的下一个元素(文件或目录)(包括 .
和 ..
除非 skip_navigational
被给予 list_dir_begin )
返回文件或目录的名称(而不是其完整路径)。一旦流被完全处理,该方法返回一个空字符串并自动关闭流(即 list_dir_end 在这种情况下不是强制性的)。
int get_space_left ( )
在UNIX桌面系统上,返回当前目录磁盘上的可用空间。在其他平台上,此信息不可用,方法返回0或-1。
初始化用于列出所有文件和目录的流,方法是 get_next 函数,根据需要关闭当前打开的流。处理流后,通常应使用 list_dir_end .
如果你通过 skip_navigational
, then .
和 ..
会被过滤掉。
如果你通过 skip_hidden
,则隐藏的文件将被过滤掉。
void list_dir_end ( )
关闭当前用打开的流 list_dir_begin (是否已完全处理 get_next 或者不重要)。
创建目录。参数可以是相对于当前目录或绝对路径的。目标目录应该放在已经存在的目录中(要递归地创建完整路径,请参见 make_dir_recursive )
The method returns one of the error code constants defined in @GlobalScope (OK
, FAILED
or ERR_*
).
通过调用 make_dir 递归地。参数可以是相对于当前目录或绝对路径的。
Returns one of the error code constants defined in @GlobalScope (0K
, FAILED
or ERR_*
).
打开文件系统的现有目录。这个 path
参数不能在项目树中 (res://folder
)用户目录 (user://folder
)或用户文件系统的绝对路径(例如 /tmp/folder
或 C:\tmp\folder
)
The method returns one of the error code constants defined in @GlobalScope (OK
or ERR_*
).
删除目标文件或空目录。参数可以是相对于当前目录或绝对路径的。如果目标目录不为空,操作将失败。
Returns one of the error code constants defined in @GlobalScope (OK
or FAILED
).
重命名(移动) from
文件发送至 to
目的地。两个参数都应该是指向文件的路径,无论是相对的还是绝对的。如果目标文件存在且不受访问保护,它将被覆盖。
Returns one of the error code constants defined in @GlobalScope (OK
or FAILED
).