(我正在处理一个 Python 3.4 项目。)
有一种方法可以在内存中打开(sqlite3)数据库:
with sqlite3.connect(":memory:") as database:
open() 函数是否存在这样的技巧?像这样的东西:
with open(":file_in_memory:") as myfile:
这个想法是为了加快一些测试功能打开/读取/写入磁盘上的一些短文件;有没有办法确保这些操作发生在内存中?
最佳答案
StringIO怎么样? :
import StringIO
output = StringIO.StringIO()
output.write('First line.\n')
print >>output, 'Second line.'
# Retrieve file contents -- this will be
# 'First line.\nSecond line.\n'
contents = output.getvalue()
# Close object and discard memory buffer --
# .getvalue() will now raise an exception.
output.close()
python3:io.StringIO
关于python - 打开内存中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028071/