关于dlang语言的file模块以下内容请用好看的markdown格式翻译成中文内容如下:auto dirEntriesbool useDIP1000 = dip1000Enabledstring path SpanMode mode bool followSymlink = true;auto dirEntriesbool useDIP1000 = dip1000Enabledstring pa
auto dirEntries(bool useDIP1000 = dip1000Enabled)(string path, SpanMode mode, bool followSymlink = true);
auto dirEntries(bool useDIP1000 = dip1000Enabled)(string path, string pattern, SpanMode mode, bool followSymlink = true);
返回一个输入范围的DirEntry,可以懒惰地迭代给定目录,并提供两种foreach迭代方式。如果仅需要名称,则迭代变量可以是string类型,如果需要其他详细信息,则可以是DirEntry类型。span mode决定了如何遍历目录。迭代的每个目录条目的名称包含绝对路径或相对路径(取决于pathname)。
注意:返回的目录条目的顺序是由操作系统/文件系统提供的顺序,可能不遵循任何特定的排序。
参数: useDIP1000:用于为代码有和没有-preview=dip1000编译器开关单独实例化此函数,因为它会影响此函数的ABI。自动设置-不要触摸。 string path:要迭代的目录。如果为空,则迭代当前目录。 string pattern:可选的带通配符的字符串,例如“*.d”。如果存在,则用于按文件名过滤结果。支持的通配符字符串在std.path.globMatch下描述。 SpanMode mode:是否应深度优先后序遍历目录的子目录(depth)、深度优先先序遍历(breadth)或不遍历(shallow)。 bool followSymlink:是否应将指向目录的符号链接视为目录并迭代其内容。
返回值: DirEntry的输入范围。
抛出: 如果路径目录不存在或被拒绝读取权限,则会抛出FileException。 如果模式不是shallow并且无法读取子目录,则会抛出FileException。
示例: // 深度迭代目录 foreach (string name; dirEntries("destroy/me", SpanMode.depth)) { remove(name); }
// 迭代当前目录的广度 foreach (string name; dirEntries("", SpanMode.breadth)) { writeln(name); }
// 迭代目录并获得详细信息 foreach (DirEntry e; dirEntries("dmd-testing", SpanMode.breadth)) { writeln(e.name, "\t", e.size); }
// 迭代当前目录及其所有子目录中的所有*.d文件 auto dFiles = dirEntries("", SpanMode.depth).filter!(f => f.name.endsWith(".d")); foreach (d; dFiles) writeln(d.name);
// 将其与std.parallelism连接,以并行编译它们: foreach (d; parallel(dFiles, 1)) //每个线程只传递一个文件 { 字符串cmd = “dmd -c“~ d.name; writeln(cmd); std.process.executeShell(cmd); }
// 迭代当前目录及其所有子目录中的所有D源文件 auto dFiles = dirEntries("","*.{d,di}",SpanMode.depth); foreach (d; dFiles) writeln(d.name);
使用SpanMode.shallow处理具有拒绝读取权限的子目录: void scan(string path) { foreach (DirEntry entry; dirEntries(path, SpanMode.shallow)) { try { writeln(entry.name); if (entry.isDir) scan(entry.name); } catch (FileException fe) { continue; } //忽略 } }
scan("");
示例: 与D1的std.file.listdir()重复功能: string[] listdir(string pathname) { import std.algorithm.iteration : map, filter; import std.array : array; import std.path : baseName;
return dirEntries(pathname, SpanMode.shallow)
.filter!(a => a.isFile)
.map!((return a) => baseName(a.name))
.array;
}
//仅在-preview=dip1000的情况下才能安全使用 @safe void main(string[] args) { import std.stdio : writefln;
string[] files = listdir(args[1]);
writefln("%s", files);
}
@safe ulong getAvailableDiskSpace(scope const(char)[] path);
根据给定的路径返回可用磁盘空间。在Windows上,path必须是目录;在POSIX系统上,它可以是文件或目录。
参数: const(char)[] path:在Windows上,它必须是目录;在POSIX系统上,它可以是文件或目录。
返回值: 可用空间(以字节为单位)
抛出: 如果失败,则会抛出FileException。
示例: import std.exception : assertThrown;
auto space = getAvailableDiskSpace("."); assert(space > 0);
assertThrown!FileException(getAvailableDiskSpace("ThisFileDoesNotExist123123"));
@trusted string tempDir();
返回一个用于临时文件的目录路径。在POSIX平台上,它通过以下目录列表进行搜索,并返回找到的第一个存在的目录:
TMPDIR环境变量指定的目录。 TEMP环境变量指定的目录。 TMP环境变量指定的目录。 /tmp/ /var/tmp/ /usr/tmp/
在所有平台上,如果失败,则tempDir返回当前工作目录。
该函数的返回值被缓存,因此下面描述的过程只会在第一次调用函数时执行。所有随后的运行将返回相同的字符串,无论环境变量和目录结构是否在此期间更改了。
POSIX tempDir算法受到Python的tempfile.tempdir的启发。
返回值: 在Windows上,此函数返回调用Windows API函数GetTempPath的结果。
在POSIX平台上,它通过以下目录列表进行搜索,并返回找到的第一个存在的目录:
TMPDIR环境变量指定的目录。 TEMP环境变量指定的目录。 TMP环境变量指定的目录。 /tmp /var/tmp /usr/tmp
在所有平台上,如果失败,则tempDir返回“。”,表示当前工作目录。
示例: import std.ascii : letters; import std.conv : to; import std.path : buildPath; import std.random : randomSample; import std.utf : byCodeUnit;
// 随机id,带有20个字母 auto id = letters.byCodeUnit.randomSample(20).to!string; auto myFile = tempDir.buildPath(id ~ "my_tmp_file"); scope(exit) myFile.remove;
myFile.write("hello"); writeln(myFile.readText); // "hello"
@safe ulong getAvailableDiskSpace(scope const(char)[] path);
根据给定的路径返回可用磁盘空间。在Windows上,path必须是目录;在POSIX系统上,它可以是文件或目录。
参数: const(char)[] path:在Windows上,它必须是目录;在POSIX系统上,它可以是文件或目录。
返回值: 可用空间(以字节为单位)
抛出: 如果失败,则会抛出FileException。
示例: import std.exception : assertThrown;
auto space = getAvailableDiskSpace("."); assert(space > 0);
assertThrown!FileException(getAvailableDiskSpace("ThisFileDoesNotExist123123"))
原文地址: http://www.cveoy.top/t/topic/gSIG 著作权归作者所有。请勿转载和采集!