如何创建虚拟文件进行输出? [英] How to create a virtual file for output?
问题描述
我需要在内存中创建一个虚拟文件,以欺骗系统并让它认为它存在.
I need to create a virtual file in memory to trick the system and to let it think that it exists.
我有一些科学程序,我希望提供一些功能文件作为参数输出.
I have some scientific program and I heed to provide in some function file for output as argument.
我想在内存中创建一些文件并将其提供给此功能.
I want to create some file in memory and provide this file to this function.
我不想使用某些临时文件夹,等等.您如何看待,是否有可能创建此技巧?
I do not want to use some temp folders and so on. How do you think, is it possible to create this trick?
推荐答案
<代码> io 库可以创建类似于对象的内存文件,这些对象的行为类似于使用典型的 f = open(filename,mode)
打开的文件句柄.如果您将以普通文本模式打开,则 StringIO
应该可以解决问题,或者如果您已经以二进制模式打开文件,请
The io
library can create in-memory file like objects that act like file handles opened with the typical f = open(filename, mode)
. If you would have opened in normal text mode, a StringIO
should do the trick, or if you would have opened the file in binary mode, BytesIO
is the way to go.
这篇关于如何创建虚拟文件进行输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!