用 python 将文件表示为对象
问题:如何将 python 中的文件表示为一个对象,完全在内存中,而不调用硬盘驱动器?
信息:
我正在开发一个项目,该项目的文件同时分布在多台计算机上,这些文件都存储在带有标识符的 SQLite3 数据库中,以便文件可以保持同步,并且我只需要处理 1 个文件在计算机上而不是很多。
我的问题是“打开”命令需要硬盘驱动器上的路径。那么这条路不存在。不过,我仍然需要能够与这些文件对象进行交互。我正在寻找一种与这些文件交互的方法,就像它们在硬盘驱动器上一样,但它们仅在内存中,可能作为字节字符串。非常好,就像我要去一样:
file = open(<location in memory>,'r')
我尝试过搜索此内容,但所有搜索结果都仅指向硬盘驱动器中的流文件。所以为了澄清这一点,我不会从硬盘驱动器进行流式传输,这不是一个选择。如果我必须这样做,我会为此重新设计我的系统。但现在这是一个额外的毫无意义的步骤。
Questions: How do you represent a file in python as an object, entirely in memory, making no calls to the hard drive?
Info:
I'm working on a project that has files distributed across many computers at once, these files are all stored in a SQLite3 database with identifiers so that the files can be keep in sync and I only have to have 1 file to deal with on the computers rather than many.
My problem is that the "open" command requires a path on the hard drive. well that path doesn't exist. I still need to be able to interact with these file objects though. What I'm looking for is a way to interact with these files as if they were on the hard drive, but they are only in memory, probably as a byte string. so kind like if i were to go:
file = open(<location in memory>,'r')
I've tried searching for this, but all search results just point to streaming files from the hard drive. so just to make this clear, I am no streaming from the hard drive, and that is not an option. If I have to do that, I'll rework my system for that. but right now that is an extra pointless step.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 StringIO(和 cStringIO)模块: http://docs.python.org/库/stringio.html
Take a look at the StringIO (and cStringIO) module: http://docs.python.org/library/stringio.html