Fuji
Fuji

Reputation: 29864

Override file operations in Python

Is there 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.

Upvotes: 2

Views: 1235

Answers (4)

ThiefMaster
ThiefMaster

Reputation: 318598

If you just need file objects which do not have real files behind them, have a look at the StringIO module.

Upvotes: 2

Fuji
Fuji

Reputation: 29864

I found this which may be close enough to do the job code.google.com/p/pyfilesystem

Upvotes: 0

vz0
vz0

Reputation: 32923

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.

Upvotes: 0

Lennart Regebro
Lennart Regebro

Reputation: 172309

"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.

Upvotes: 1

Related Questions