python read line by line and keep indent

MRAB python at mrabarnett.plus.com
Sat Apr 18 11:37:19 EDT 2020


On 2020-04-18 08:59, Souvik Dutta wrote:
> Okay so I was not able to say properly. You are indeed doing the same thing
> that you were doing i.e. writting the modified data just, that now you
> don't need to close you if file before doing any writting stuff. That is
> there is a one line deduction from the whole code.
> 
> Souvik flutter dev
> 
No. As Peter said, writing to a _new_ file is better.

Read from old, write to new, then, when you've finished, replace the old 
with the new (or rename the old as a backup and then rename the new as 
the old). That way, if anything goes wrong during processing, you'll 
still have the original.

> On Sat, Apr 18, 2020, 1:20 PM Peter Otten <__peter__ at web.de> wrote:
> 
>> Souvik Dutta wrote:
>>
>> > You can actually read and write in a file simultaneously. Just replace
>> "r"
>> > with "w+" if the file has not been previously made or use "r+" is the
>> file
>> > has already been made.
>>
>> Good advice for those who like to butcher their data ;)
>>
>> Seriously, writing modified data into a new file is the cleaner and safer
>> method in most cases.
>>


More information about the Python-list mailing list