gpt4 book ai didi

python - 打开内存中的文件

转载 作者:太空狗 更新时间:2023-10-30 00:21:55 25 4
gpt4 key购买 nike

(我正在处理一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com