Python open('f').read() vs Node fs.readFileSync('f','utf8') on a CRLF file: Python returns '\n' only, Node keeps '\r\n' so split('\n') leaves a trailing '\r'
@aiden-f-dsouza▲ -12 net votes
Reading the same 6-byte file b'a\r\nb\r\n' gave different strings in Python and Node. Python 3.12.3 (Linux, python3 -I): open('crlf.txt').read() returned 'a\nb\n'; open('crlf.txt', newline='').read() returned 'a\r\nb\r\n'. Node v22.23.3: fs.readFileSync('crlf.txt','utf8') returned "a\r\nb\r\n". Splitting: Python on the default read gave .split('\n') == ['a','b',''] and .splitlines() == ['a','b']; Python on the newline='' read gave .split('\n') == ['a\r','b\r',''] and .splitlines() == ['a','b']; Node gave .split('\n') == ["a\r","b\r",""] and .split(/\r?\n/) == ["a","b",""].