Reputation: 2124
Is it possible to create files in memory, and arrange them in a type of hierarchy before writing them to disk?
Can an open
statement be redirected into some kind of in-memory representation?
My current technique for creating the zipped directories is this:
zipfile
objectUltimately ending up with something like this:
Zipped_root
|
|
|---- file1.txt
|
|---- Image1.png
|
|---- Image2.png
|
|---- file...N.txt
|
Is there a way to do this all in memory?
Upvotes: 1
Views: 996
Reputation: 9077
A while ago I implemented a small python library (https://github.com/kajic/vdir) for creating virtual directories, files and even compressing them if need be. From the README (the virtual directory is compressed at the end):
from vdir import VDir
vd = VDir()
# Write to file
vd.open("path/to/some/file").write("your data")
# Create directory, go inside it, and write to some other file
vd.mkdir("foo")
vd.cd("foo")
vd.open("bar").write("something else") # writes to /foo/bar
# Read from file
vd.open("bar").read()
# Get the current path
vd.pwd()
# Copy directory and all its contents
vd.cp("/foo", "/foo_copy")
# Move the copied directory somewhere else
vd.mv("/foo_copy", "/foo_moved")
# Create a file, then remove it
vd.open("unnecessary").write("foo")
vd.rm("unnecessary")
# Walk over all directories and files in the virtual directory
vd.cd("/")
for base, dirnames, dirs, filenames, files in vd.walk():
pass
# Recursively list directory contents
vd.ls()
# Create a zip from the virtual directory
zip = vd.compress()
# Get zip data
zip.read()
I just did it for fun and haven't tested it extensively, but perhaps it can be of use to you anyway.
Upvotes: 3
Reputation: 4399
Yes. Take a look at the zlib module documentation on compressing objects. There is also an archiving module that you can used to create the archive object to be compressed. You can access the documentation at any time:
$ python
>>> import zlib
>>> help(zlib)
Upvotes: -1