Assertion error when attempting to read a virtual file.

I’m trying to read a collection of yaml files in a directory without hardcoding it. VirtualFile.readFile(bool) is throwing an assertion error and I am not sure how I am doing it wrong.

vfs = VirtualFileSystem.getGlobalPtr()
textFileList = vfs.scanDirectory('text')

i = 0
while i < textFileList.size():
    print "Reading " + str(textFileList[i])
    content = textFileList[i].readFile(True)
    i += 1

Output:

Reading /home/croxis/src/OpenOutpost/text/TXT_AI_Prelaunch.yaml
python: dtool/src/dtoolutil/filename.cxx:2266: bool Filename::open_read(pifstream&) const: Assertion `is_binary_or_text()' failed.
Aborted

scanDirectory also returns subdirectories, and you’re trying to pass a directory to readFile.

But the file being read is a regular file (its in the output line). I’ve also added an isRegularFile() check too with the same issue

while i < textFileList.size():
    print "Reading " + str(textFileList[i])
    if textFileList[i].isRegularFile():
        content = textFileList[i].readFile(True)
        i += 1

Odd. Does it help if you do setText() on the Filename first?

That would be a good idea, but at this point the only Filename I am using is for the “text” directory. vfs.scanDirectory returns a VirtualFileList, which returns VirtualFiles, none of which have a setText function.

textDirectoryName = Filename('text') # This is a directory with *.yaml files
textFileList = vfs.scanDirectory(textDirectoryName)
i = 0
while i < textFileList.size():
    print "Reading " + str(textFileList[i]) # Shows the full and correct path to the yaml file
    if textFileList[i].isRegularFile():
        content = textFileList[i].readFile(True)
        i += 1

This may be a bug in vfs.scanDirectory(). Can you post a bug report on the launchpad please?

Thanks!
David