Override file operations in Python
Is there开发者_JAVA百科 a way to override all file operations in Python? File operations such as 'open', 'os.rename' and 'os.unlink'.
I want to create a temporary, in-memory file system without rewriting a library. Does anyone know of a script or a library that has this feature? I want to run a library on Google App Engine and it is not possible to write to the file system.
If you just need file objects which do not have real files behind them, have a look at the StringIO module.
"Override"? That word doesn't really make any sense in that context. You can replace them in various ways, depending on what you want really.
Making a in memory file system can be done on most OS's through the operating system, like with tmpfs in most Unices. That's probably a better solution for you.
If you want to write the code of your own file system from scratch, one way or another you will end up rewriting a library.
I have no knowledge of any library implementing an in-memory temporary file system in pure Python. As an alternative, in Linux you can use the tmpfs
file system, usually mounted on /dev/shm
. You can open, read and write files there as usual.
I found this which may be close enough to do the job code.google.com/p/pyfilesystem
精彩评论