[Tutor] A Multiple Concatenation Problem
Cameron Simpson
cs at cskk.id.au
Fri Sep 18 18:43:37 EDT 2020
On 18Sep2020 09:34, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>I read the data in with:
>
>filename = 'Ligand.list'
>file = open(filename,mode='r')
>text = file.read()
>file.close()
Others are debugging your filename composition, I just wanted to mention
the above bit of code.
The idiomatic way to write this is:
with open(filename) as file:
text = file.read()
Aside from being shorter, there are two things to note here:
1: The default open mode is 'r' (read as text), so you don't need to say
that.
2: The with... construction closes the file for you, and in fact ensures
that the close happens even if there is an exception. This ensures
timely file close and reliable file close.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list