I have a python script that reads the temp froma ds18b20 sensor and writes it to a sqlite3 db located in the same directory. The section of code that writes to the database looks like this:
def write_DB(devID, devNAME, temp):
conn = sqlite3.connect('templog.db')
curs = conn.cursor()
curs.execute("INSERT INTO temps values((?),(?), datetime('now'), (?))", (devID, devNAME, temp))
conn.commit()
When run from idle3 it runs fine, but when run from the command line, or cron, this is the output:
~/python/HydroMon $ /usr/bin/python /home/pi/python/HydroMon/ds18b20_scanner.py
Traceback (most recent call last):
File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 102, in <module>
scan_dev()
File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 69, in scan_dev
write_DB(file, dev, temp)
File "/home/pi/python/HydroMon/ds18b20_scanner.py", line 49, in write_DB
curs.execute("INSERT INTO temps values((?),(?), datetime('now'), (?))", (devID, devNAME, temp))
sqlite3.OperationalError: unable to open database file
Any idea why this won't run from the command line? All the permissions are correct.
EDIT: I added this to write_db()
global basedir
db_file = basedir + '/templog.db'
conn = sqlite3.connect(db_file)
This no longer produces and error. This is the basedir assignment function to open and read a conf file, which now causes an error. I have tried with python and python3.
#Configuratio Directory
fullpath = sys.argv[0]
basedir = os.path.dirname(fullpath)
confdir = basedir + "/conf"
#Variable to check if scan was good
reading = 0
def get_dev_name(devID):
global confdir
devName = 'NONE'
#open conf file
conf = confdir + '/devname.conf'
print(conf)
cfile = open(conf,mode='r')
When run from idle3 the result is: /home/pi/python/HydroMon/conf/devname.conf
When run from the command line:
~/python/HydroMon $ sudo python3 ds18b20_scanner.py
/conf/devname.conf
Traceback (most recent call last):
File "ds18b20_scanner.py", line 105, in <module>
scan_dev()
File "ds18b20_scanner.py", line 70, in scan_dev
dev = get_dev_name(file)
File "ds18b20_scanner.py", line 27, in get_dev_name
cfile = open(conf,mode='r')
IOError: [Errno 2] No such file or directory: '/conf/devname.conf'