If taken into consideration that carriage return = \r
and line feed = \n
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> '{:02x}'.format(ord('\n'))
'0a'
>>> '{:02x}'.format(ord('\r'))
'0d'
how to avoid writing carriage return when using open('filename','w').write('text\n')
?
In interactive mode you can do this:
>>> open('filename','w').write('text\n')
5
>>> for c in open('filename','r').read():
... print('{:02x}'.format(ord(c)))
...
74
65
78
74
0a
This would indicate that only line feed has been written, thus it should be 5 bytes long.
-rw-r--r-- 1 djuric 197121 6 Jul 15 21:00 filename
^
It is actually 6 bytes long. Now this can be a "Windows thing", but when you open the file in Notepad++ for example, and you turn View > Show Symbols > Show All Characters you can see the carriage return there.
After pressing CTRL+H and replacing \r with nothing using Extended Search Mode, only line feed is left. After saving the file, only line feed is in the file and the file is 5 bytes long.
-rw-r--r-- 1 djuric 197121 5 Jul 15 20:58 filename1
^
So why is Notepad++ able to save line feeds without carriage return, but python can't?