Skip to content

Commit 324cc2b

Browse files
committed
modules/tools/DataLog: no headers when appending
When appending data to a non-empty file, skip the headers.
1 parent cb72ea5 commit 324cc2b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

bricks/ev3dev/modules/pybricks/tools.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
# Import print for compatibility with 1.0 release
55
from builtins import print
6+
7+
# Expose method and class written in C
68
from tools import wait, StopWatch
9+
10+
# Imports for DataLog implementation
711
from utime import localtime, ticks_us
812

913

@@ -24,8 +28,12 @@ def __init__(self, *headers, name='log', timestamp=True, extension='csv', append
2428
# Append extension and open
2529
self.file = open('{0}{1}.{2}'.format(name, stamp, extension), mode)
2630

27-
# If column headers were given, print those as first line
28-
if len(headers) > 0:
31+
# Get length of existing contents
32+
self.file.seek(0, 2)
33+
length = self.file.tell()
34+
35+
# If column headers were given and we are at the start of the file, print headers as first line
36+
if len(headers) > 0 and length == 0:
2937
print(*headers, sep=', ', file=self.file)
3038

3139
def log(self, *values):

0 commit comments

Comments
 (0)