Skip to content

Commit 8097a97

Browse files
committed
modules/tools/DataLog: allow appending if exists
Add ability to append data to an existing data log.
1 parent 275cdc9 commit 8097a97

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

bricks/ev3dev/modules/pybricks/tools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class DataLog():
11-
def __init__(self, *headers, name='log', timestamp=True, extension='csv'):
11+
def __init__(self, *headers, name='log', timestamp=True, extension='csv', append=False):
1212

1313
# Make timestamp of the form yyyy_mm_dd_hh_mm_ss_uuuuuu
1414
if timestamp:
@@ -18,8 +18,11 @@ def __init__(self, *headers, name='log', timestamp=True, extension='csv'):
1818
else:
1919
stamp = ''
2020

21+
# File write mode
22+
mode = 'a+' if append else 'w+'
23+
2124
# Append extension and open
22-
self.file = open('{0}{1}.{2}'.format(name, stamp, extension), 'w+')
25+
self.file = open('{0}{1}.{2}'.format(name, stamp, extension), mode)
2326

2427
# If column headers were given, print those as first line
2528
if len(headers) > 0:

0 commit comments

Comments
 (0)