Skip to content

Commit f1df887

Browse files
bstrinerfchollet
authored andcommitted
Fix CSV formatting for Windows with Python 2 (#6311)
* Fix CSV formatting for windows with python 2 * Fix pep8 whitespace * Fix quote style
1 parent 0ddc336 commit f1df887

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

keras/callbacks.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import csv
6+
import six
67

78
import numpy as np
89
import time
@@ -861,16 +862,17 @@ def __init__(self, filename, separator=',', append=False):
861862
self.writer = None
862863
self.keys = None
863864
self.append_header = True
865+
self.file_flags = 'b' if six.PY2 and os.name == 'nt' else ''
864866
super(CSVLogger, self).__init__()
865867

866868
def on_train_begin(self, logs=None):
867869
if self.append:
868870
if os.path.exists(self.filename):
869-
with open(self.filename) as f:
871+
with open(self.filename, 'r' + self.file_flags) as f:
870872
self.append_header = not bool(len(f.readline()))
871-
self.csv_file = open(self.filename, 'a')
873+
self.csv_file = open(self.filename, 'a' + self.file_flags)
872874
else:
873-
self.csv_file = open(self.filename, 'w')
875+
self.csv_file = open(self.filename, 'w' + self.file_flags)
874876

875877
def on_epoch_end(self, epoch, logs=None):
876878
logs = logs or {}

0 commit comments

Comments
 (0)