File writing

I’ve gathered that Python has file-handling commands, but it also appears that Panda has its own file-handling system. Wanting to perform some file writing (of a fairly basic file, not an egg or bam, for which there seem to be specialised classes), and finding the use of Panda’s system more attractive, I tried last night (technically this morning) to figure out how to write out to a file using it.

Unfortunately, this didn’t go terribly well.

After a variety experimentations, forum searches, local searches for and within files and source code skimming, I still have little idea of how to go about it. It looks as though it might involve some or all of VirtualFileSystem, VirtualFile, Filename, StringStream and StreamWriter.

For one thing, while there appear to be a number of methods involved in reading files, there seem to be fewer that look as though they write to files.

According to the documentation, and supported, as I recall, (albeit not fully confirmed) by source code, Filename should have the methods openAppend, openRead, openReadWrite and openWrite. None of which exist, according to SPE, at least as I’m using them.

When used like so:

fn = Filename("cat.txt")

I receive an error of the form:

Does Panda have file writing functionality, or should I turn to built-in python commands and/or Pickle? If Panda does have this functionality, how do I use it?

Panda’s file-writing library interface is primarily designed for C++ coders. It doesn’t exist on the Python side (because the C++ standard ofstream class is not exposed to Python). But Python’s native file-writing interfaces are quite nice.

Panda’s file-reading library interface does exist on the Python side, in order to support Panda’s virtual file system (which allows you to mount multifiles as if they are directory structures, and then read files from within the multifiles as if they are real files). However, Panda’s virtual file system has not yet been extended to include writing to multifiles, so we haven’t bothered to expose the system to Python. Maybe one day we will add this feature, and then it will become useful to have access to this interface in Python.


Aah, I see - thank you.

Based on the manual - both in terms of appearance and of the presence of seemingly-appropriate structures and methods - I got the impression that the system was intended to include such functionality.

Should I perhaps add something to the manual somewhere to indicate that file writing isn’t available in Python? I’d like to reduce the number of others that might end up in as frustrating a hunt as I had last night…

Otherwise, indeed, Python’s file-handling systems look pretty decent, and should serve my purposes.

Thank you again! :slight_smile:

Sounds reasonable. But what in the manual gave you the impression to the contrary?


I think that it was a combination of factors.

To start with, the openWrite, openAppend and openReadWrite (if I recall correctly) methods in Filename looked promising, as did StringStream and StreamWriter (although I realise that the latter two can, and presumably are, used to write to other targets than files).

The existence of dedicated writing systems, such as those used to write eggs or Multifiles, suggested, I think, the possibility of a more general writing system beneath them, which I think was supported by the presence of a virtual file system.

I felt, I believe that it was reasonable to assume that, unless otherwise indicated, the presence of a file system capable of reading implies, to some extent, at least, the presence of write-functionality. In other words, since the file system doesn’t indicate that it’s uni-directional for Python (either in name or text), it seemed reasonable to guess that it was bi-directional.

As I recall, I reasoned that the lack of appropriate methods in the documentation could simply be due to a lack of their inclusion, rather than existence, or my looking in the wrong place.

I also forgot that the reference also serves a separate (or superset?) C++ API.

Finally, It’s probably also worth noting that I was pretty tired last night; it’s possible that I might not have banged my head against the problem for quite so long and quite so stubbornly if not for that.

I think that that’s everything. ^^;

All perfectly reasonable. You’re right, it would be helpful to have a tip in the manual to advise the user.

It’s not clear where this tip should appear, though. Maybe we need a new manual page dedicated to file reading (via the VFS), and this page can mention that file writing via the VFS is not presently supported.


Fair enough - I’ll try to remember to do that tonight, then. :slight_smile:

(Although, since I haven’t used the VirtualFileSystem’s reading capabilities thus far, I’ll leave that undone for now, I think.)

My original thought was to add short caveats to the descriptions of Filename and VirtualFileSystem, as I recall - shall I do that as well?

Sure. I do want to try to avoid the overt wikification of the manual, though. So many wikis tend degenerate into a disorganized laundry list of “gotchas” on every page, which is really a poor kind of organization for a manual that’s intended to serve as both a tutorial and a reference guide.

I’ll trust in your judgment to place disclaimers in such a way that they will be useful without interfering with structure or learning. :slight_smile:


Fair enough - I’ll try to get to it later tonight, when I’m hopefully feeling a little more wakeful, and trust my own judgement a little more. :wink:

All right, I’ve added a “File Reading” section to the manual, placed after the section on Multifiles, as section II - AC.

I provided a little bit of introduction, and included my caveat about file writing, as well as an incompleteness notice for the page. I didn’t attempt to write much regarding file reading, as I haven’t attempted to use Panda’s file reading capabilities yet, I don’t think.

I would appreciate it if someone would take a look, please, and check for any errors or problems. ^^;

Finally, I notice now that the reference isn’t part of the wiki, as I had at first thought, meaning that I don’t seem to have editing access in order to add the caveats that I mentioned for the Filename and VirtualFileSystem pages.

[edit] Oh, and my apologies that I took so long to get to this. ^^;