I'm writing a series of SQL statements to a file using python. The template string looks like:
store_insert = '\tinsert stores (storenum, ...) values (\'%s\', ...)'
I'm writing to the file like so:
for line in source:
line = line.rstrip()
fields = line.split('\t')
script.write(store_insert % tuple(fields))
script.write(os.linesep)
However, in the resulting output, I see \r\r\n at the end of each line, rather than \r\n as I would expect. Why?
\n
is converted to os.linesep
for files opened in text-mode. So when you write os.linesep
to a text-mode file on Windows, you write \r\n
, and the \n
gets converted resulting in \r\r\n
.
See also the docs:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.