如何获取D2中File.tmpfile创建的临时文件的名称?
我需要生成一个临时文件,用一些数据填充它并将其提供给外部程序。根据此处提供的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 tmpfile() 的工作原理,如果您需要文件名,则无法使用它。但是,我已经创建了一个处理临时文件的模块。它使用条件编译来决定查找临时目录的方法。在 Windows 上,它使用 %TMP% 环境变量。在 Posix 上,它使用 /tmp/。
该代码已获得 WTFPL 许可,因此您可以用它做任何您想做的事情。
要使用它,只需使用您想要的任何参数调用 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.
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.