readText / saveText

@sim @John Why does fileName work with saveText but not with readText.

fileName points to a file that doesn’t exist yet. saveText creates it, but readText ends up nil. If I comment out the saveText line, it still doesn’t work.

function setup()
    fileName=asset.documents.Dropbox.."text1"   -- file doesnt exist yet

    print("text1.. ",val)

That’s a good question @dave1707, I can only assume its something to do with the asset system not recognising the newly created file? Do you know if it does this with other asset types and save/read calls?

Thanks for the bug report!

@John I don’t think the problem is with a newly created file. I can’t seem to find the correct naming format for the file name if the file doesn’t exits yet and I want to create it and later read it in the code.

The below code works if I hardcode the file name even though I get an editor error message on the readText line saying the file doesn’t exist yet.

I can’t seem to find the correct format if I want to make the file name (text1) a variable in those 2 lines.

function setup()
    print("text1.. ",val)

@John I finally found the correct combination to create then read a file using a variable name.

The below code seems to work.

function setup()
    print("text1.. ",val)

@John The above code works OK, but if the file doesn’t exist and I comment out the saveText and just do the readText on a non existent file, Codea kicks out an error.

What I’m trying to do is:

  1. Have the file name in a variable.
  2. Read the file and if it doesn’t exist, create an empty file.
  3. Or read the file and if it exists, process the info I read.

The above code would work if Codea returned a nil when trying to read a non existent file instead of kicking out an error.

I think part of the issue is how Codea’s assets system handles file extensions. So when you use the .. operator to append a filename to an existing path, internally its just adding the string. But when you access an existing file it will automatically find the file and guess the type based on the extension.

So when you do asset.documents.Dropbox..fname you get a file path with no extension. Once you save it as text it gets given a .txt extension and these don’t match. That’s why if you save two different file types with the same name the autocomplete will give you .file1_txt / .file1_png

I think the current way it works is pretty confusing and there isn’t a good way to handle non-existent files. Maybe nil is better, or maybe asset paths need some extra stuff to deal with these edge cases

Alternatively you could use Lua’s built in file io.
Though I’m not sure if this would workaround any additional processing for Dropbox files within Codea’s API? @John?

function setup()
    local fname="text1"
    local fasset = asset.documents.Dropbox .. fname
    -- Write to file (created if it doesn't exist)
    local fhandle =, "w")
    if fhandle ~= nil then
    -- read file (if it exists)
    fhandle =, "r")
    if fhandle ~= nil then
        print("text1...", fhandle:read("*a"))

@Steppers @John Using the file io functions is one way around it, but readText still has a problem. Currently readText returns a nil which is correct, but it also exits the program with the nil error message. If readText would just return the nil without the error message that would fix everything.

We could just remove the error from it and return nil, maybe replace the error with a warning?

@John Returning nil without the error message would take care of the problem. That way the program would know the file doesn’t exist and could create an empty file.