[Tutor] Remove soft line break

Valerio Pachera valerio at pbds.eu
Sat Mar 16 13:14:20 EDT 2019



----- Messaggio originale -----
> Da: "Valerio Pachera" <valerio at pbds.eu>
> A: "Tutor Python" <tutor at python.org>
> Inviato: Giovedì, 28 febbraio 2019 13:05:27
> Oggetto: Re: [Tutor] Remove soft line break

> ...
> I noticed that the end of file doesn't get preserve if I create a copy of the
> file ...

I've been told by a collegue that the when the file is opened by interpreter it uses the end of line of the system the program runs on.
In my case the os is linux, so python uses '\n' a end of line, no matter what's written in the file.
The end of line, ore better "newline" may be chosen when open the file.

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

So, if I have this string

s = '''
hallo, this is 
a multi line text
'''

I can save it in a text file for windows this way:

f = open('test.txt', 'w', newline='\r\n')
f.write(s)
f.close()



More information about the Tutor mailing list