Read .txt file in plugin

#1

Hi,

I am creating a plugin for thermal cam. I need to read some calibration values, which are stored in calibration.txt in OctoPrint-Myplugin/octoprint_myplugin/calibration.txt

open("calibration.txt").read() is not working. I am getting error "IOError: [Errno 2] No such file or directory: 'calibration.txt' "

Any ideas how to read data from .txt?

#2

Maybe something like this:

        with open(target_file_path, 'r') as f:
            while True:
                line = f.readline()
                if line == '':
                    break
                # do your processing here

You might need to include a full path, and not just the file name here depending on where your file is located. You can get your plugin data path from Octoprint pretty easily, and it's a good idea to put your txt file in that data directory (on the pi: home/pi/.octoprint/data/{plugin_name})

#3

This is a nonanswer, but here are some places to look:

octoprint/src/octoprint/plugins$ egrep -rl "open\(" *
announcements/__init__.py
appkeys/__init__.py
backup/__init__.py
errortracking/static/js/sentry.min.js
logging/__init__.py
pi_support/__init__.py
pluginmanager/__init__.py
pluginmanager/static/js/pluginmanager.js
softwareupdate/__init__.py
softwareupdate/version_checks/git_commit.py
softwareupdate/cli.py
softwareupdate/scripts/update-octoprint.py
virtual_printer/virtual.py