python - Open a file in memory -


(i'm working on python 3.4 project.)

there's way open (sqlite3) database in memory :

    sqlite3.connect(":memory:") database: 

does such trick exist open() function ? :

   open(":file_in_memory:") myfile: 

the idea speed test functions opening/reading/writing short files on disk; there way sure these operations occur in memory ?

there similar file-like input/output or string in io.stringio.

there no clean way add url-based processing normal file open, being python dynamic monkey-patch standard file open procedure handle case.

for example:

from io import stringio  old_open = open in_memory_files = {}  def open(name, mode="r", *args, **kwargs):      if name[:1] == ":" , name[-1:] == ":":           # in-memory file           if "w" in mode:                in_memory_files[name] = ""           f = stringio(in_memory_files[name])           oldclose = f.close           def newclose():               in_memory_files[name] = f.getvalue()               oldclose()           f.close = newclose           return f      else:           return old_open(name, mode, *args, **kwargs) 

after can write

f = open(":test:", "w") f.write("this test\n") f.close()  f = open(":test:") print(f.read()) 

note example minimal , doesn't handle real file modes (e.g. append mode, or raising proper exception on opening in read mode in-memory file doesn't exist) may work simple cases.

note in-memory files remain in memory forever (unless patch unlink).

ps: i'm not saying monkey-patching standard open or stringio instances idea, can :-d

ps2: kind of problem solved better @ os level creating in-ram disk. can call external programs redirecting output or input files , full support including concurrent access, directory listings , on.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -