[Tutor] Is this possible and should it be done?

Steven D'Aprano steve at pearwood.info
Mon May 21 13:33:52 CEST 2012

wolfrage8765 at gmail.com wrote:
> All, I have had a curious idea for awhile, and was wondering the best
> way to implement it in Python and if it is even possible. The concept
> is this, a file that is actually a folder that contains multiple files
> (Like an Archive format). The actual files are really un-important.

What you are describing is exactly like any one of many different file 
formats, such as zip files, tar files, and others.

> What I want is for the folder to be represented as a single file by
> any normal file browser, but to be able to access the files with-in
> via Python. I will actually use the word archive to represent my
> mystical folder as a file concept for the rest of this message. 

Actual folders ("directories") are special, since they are handled by the file 
system. But you can create any file format you like, it is just data. For 
example, a GIF file can contain multiple frames (animated GIFs); Libre Office 
and Open Office files contain multiple pieces of data; zip files can contain 
multiple compressed files of any type; AVI files can contain multiple audio 
streams; cd/dvd image files can contain multiple file system; etc.

There's nothing special about file browsers: if they don't understand a file 
format, they can't do anything special with files of that format. But if they 
do understand the file format, then they can. *Any* program that understands 
the file format can do anything it likes with the data.

> Some
> additional things I would like to be possible: is for multiple copies
> of the program to write to the same archive, but different files
> with-in at the same time (Reading & Writing to the archive should not
> lock the archive as long as they are different files); and for just
> the desired files with-in the archive to be loaded to memory with out
> having to hold the entire archive in memory.


Sure, whatever you like. You just have to program it.

> MS Word uses Binary streams as shown here:
> http://download.microsoft.com/download/5/0/1/501ED102-E53F-4CE0-AA6B-B0F93629DDC6/WindowsCompoundBinaryFileFormatSpecification.pdf
> Is this easy to do with python? Does it prevent file locking if you

File locking is not a property of the programming language, but of the file 
system and operating system.

> use streams? Is this worth the trouble, or should I just use a
> directory and forget this magical idea?

Seems like a lot of work for very little benefit, but if you want it, you can 
build it.

Personally, I think that instead of re-inventing the wheel, use existing 
tools. Python comes with the pre-built tools to use tar, zip, gzip, xml, json, 
  pickle file formats. You should investigate the powers and limitations of 
those file formats before inventing your own.

I'm sorry that I can't be more specific, but your question is awfully generic. 
It's a bit like somebody saying "Can I build a remote-controlled car out of 
electronics and metal?" Of course you can. But to actually do so, you need to 
have detailed plans rather than vague questions -- you need to know 
electronics and mechanics.

But good luck!


More information about the Tutor mailing list