如何获取D2中File.tmpfile创建的临时文件的名称?

发布于 2025-01-08 04:53:45 字数 446 浏览 2 评论 0原文

我需要生成一个临时文件,用一些数据填充它并将其提供给外部程序。根据此处提供的 D 描述,我正在使用 File.tmpfile() 方法:

auto f = File.tmpfile();
writeln(f.name());

该方法不提供获取生成文件名的方法。据记录,name 可能为空。在 Python 中,我会这样做:

(o_fd, o_filename) = tempfile.mkstemp('.my.own.suffix')

在 D2 中是否有一种简单、安全且跨平台的方法来做到这一点?

I need to generate a temporary file, fill it with some data and feed it to an external program. Based on description of D available here I'm using File.tmpfile() method:

auto f = File.tmpfile();
writeln(f.name());

which doesn't provide a way to get the generated file name. It's documented that name might be empty. In Python I would do that like this:

(o_fd, o_filename) = tempfile.mkstemp('.my.own.suffix')

Is there a simple, safe and cross-platform way to do that in D2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2025-01-15 04:53:45

由于 tmpfile() 的工作原理,如果您需要文件名,则无法使用它。但是,我已经创建了一个处理临时文件的模块。它使用条件编译来决定查找临时目录的方法。在 Windows 上,它使用 %TMP% 环境变量。在 Posix 上,它使用 /tmp/。

该代码已获得 WTFPL 许可,因此您可以用它做任何您想做的事情。

module TemporaryFiles;
import std.conv,
       std.random,
       std.stdio;

version(Windows) {
    import std.process;
}

private static Random rand;

/// Returns a file with the specified filename and permissions
public File getTempFile(string filename, string permissions) {
    string path;
    version(Windows) {
        path = getenv("TMP") ~ '\\';
    } else version(Posix) {
        path = "/tmp/";
        // path = "/var/tmp/"; // Uncomment to survive reboots
    }
    return File(path~filename, permissions);
}

/// Returns a file opened for writing, which the specified filename
public File getTempFile(string filename) {
    return getTempFile(filename, "w");
}

/// Returns a file opened for writing, with a randomly generated filename
public File getTempFile() {
    string filename = to!string(uniform(1L, 1000000000L, rand)) ~ ".tmp";
    return getTempFile(filename, "w");
}

要使用它,只需使用您想要的任何参数调用 getTempFile() 即可。默认为写权限。

请注意,“随机生成的文件名”并不是真正随机的,因为种子是在编译时设置的。

Due to how tmpfile() works, if you need the name of the file you can't use it. However, I have already created a module to work with temporary files. It uses conditional compilation to decide on the method of finding the temporary directory. On windows, it uses the %TMP% environment variable. On Posix, it uses /tmp/.

This code is licensed under the WTFPL, so you can do whatever you want with it.

module TemporaryFiles;
import std.conv,
       std.random,
       std.stdio;

version(Windows) {
    import std.process;
}

private static Random rand;

/// Returns a file with the specified filename and permissions
public File getTempFile(string filename, string permissions) {
    string path;
    version(Windows) {
        path = getenv("TMP") ~ '\\';
    } else version(Posix) {
        path = "/tmp/";
        // path = "/var/tmp/"; // Uncomment to survive reboots
    }
    return File(path~filename, permissions);
}

/// Returns a file opened for writing, which the specified filename
public File getTempFile(string filename) {
    return getTempFile(filename, "w");
}

/// Returns a file opened for writing, with a randomly generated filename
public File getTempFile() {
    string filename = to!string(uniform(1L, 1000000000L, rand)) ~ ".tmp";
    return getTempFile(filename, "w");
}

To use this, simply call getTempFile() with whatever arguments you want. Defaults to write permission.

As a note, the "randomly generated filenames" aren't truely random, as the seed is set at compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文