...
Adapted and simplified from Franck Albinet's exercises available here. Also presented by NSRC at APRICOT 2018.
Introduction
While one of the primary purpose of IoT is to collect and exchange data over an inter-connected network, it is as well important to be able to persist information in the IoT device itself: log files of device's activity, Received Signal Strength Activity (RSSI), ...
...
The try ... except
statement does the following: 1.
- try to list the files and folder under
/flash/log
folder;
...
- if the folder does not exist an error occurs (it raises an OSError exception) that we intercepts to write our message and create our folder.
"Advanced" code [optional]
When we use log files to monitor a device's activity, we often want to generate programmatically a file name in the following format: /flash/log/acq/yyyymmddhhmmsslist.csv
with: yyyy
current year; mm
current month; * ...
A first simple, explicit and readable approach could be:
Code Block | ||||
---|---|---|---|---|
| ||||
import time year, month, day, hour, minute, second, ms, dayinyear = time.localtime() nameCsv = '/flash/log/acq' nameCsv = nameCsv + '{:04d}'.format(year) nameCsv = nameCsv + '{:02d}'.format(month) nameCsv = nameCsv + '{:02d}'.format(day) nameCsv = nameCsv + '{:02d}'.format(hour) nameCsv = nameCsv + '{:02d}'.format(minute) nameCsv = nameCsv + '{:02d}'.format(second) nameCsv = nameCsv + 'list.csv' |
A second approach more succinct would be to take advantage of Python list comprehensions: