Reading text from a file

edited March 2017 in Python Mode

Hello everyone,

I am having some trouble reading text from a file. I can not seem to find any examples on the internet, and in the reference I do not find any suitable commands. There is createReader(), but that says that it doesn't find a file (I have tried putting it next to the source file as well as in the data folder, but it did not work. I will try some more in the meantime.

How should one read text from a file?

Kind regards, OrOrg

Answers

  • Unfortunately, it does not work when I do

    lines = loadStrings("Changelog.txt")
    for line in lines:
        print(line)
    

    with the changelog either next to the source file or in the data folder. This is the example in the link you referred to. The error it returns: NoneType object is not iterable, which I assume means the file was not found, thus not read, meaning the lines variable is a nonetype since it is empty.

    D:

    Is there a specific folder I have to put it in? Or do I have to import a certain library first?

  • edited March 2017 Answer ✓

    It should simply work. Perhaps check again your filename's case, whether it's "Changelog.txt" or "changelog.txt".

    Make sure the ".txt" file is in the same folder as your ".pyde" sketch, or inside its subfolder "data/".

    Hit CTRL+K in order to open ".pyde" sketch's folder. My attempt for it: B-)

    def setup():
        global logs
        logs = loadStrings('Changelog.txt')
        for l in logs: print l
        exit()
    
  • Answer ✓

    I have some good news and some bad news.

    The bad news: I am a moron. I always forgot to put that piece of code in the setup function, because I was just quickly trying it and therefore lazily put it at the top of my whole program. Even when I was looking at it for half an hour, I didn't notice.

    The good news: it now works :D

    Thank you, gotoloop.

  • Everybody makes simple mistakes. Onward and upward!

Sign In or Register to comment.