[Tutor] Remove soft line break
Valerio Pachera
valerio at pbds.eu
Thu Feb 28 09:33:45 EST 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
>...
> It works as expected but there's a thing I've to understand about the end of
> line in general.
> ...
> I noticed that the end of file doesn't get preserve if I create a copy of the
> file by simply
>
> f = open('file.ics')
> content=f.read()
> f.close()
>
> f = open('file_copy.ics', 'w')
> write('content')
> f.close()
Got it!
Python automatically convert new line to \n when it opens the file
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
This is my final script that overrides the original file.
---
import sys
f = open(sys.argv[1], newline='\r\n')
content = f.read().replace('\r\n ', '')
f.close()
f = open(sys.argv[1], 'w')
f.write(content)
f.close()
---
Valerio P.
More information about the Tutor
mailing list