Re: open, close

2019-09-01 Thread Max Zettlmeißl via Python-list
On Sat, Aug 31, 2019 at 3:43 PM Piet van Oostrum wrote: > > There is a difference here with the construct that the OP mentioned: > > lines = open("foo.txt").readlines() > > In that case the file COULD be closed, but there is no guarantee. It depends > on garbage collection. > In your case t

Re: open, close

2019-09-01 Thread Barry
> On 1 Sep 2019, at 17:57, MRAB wrote: > > On 2019-09-01 16:46, Barry wrote: >>> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: >>> When you say COULD this sounds like it is a matter of luck. My thinking >>> was that USUALLY the file will be closed after the statement because >>> then the file

Re: open, close

2019-09-01 Thread Chris Angelico
On Mon, Sep 2, 2019 at 3:02 AM MRAB wrote: > > On 2019-09-01 16:46, Barry wrote: > > > > > >> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > >> > >> When you say COULD this sounds like it is a matter of luck. My thinking > >> was that USUALLY the file will be closed after the statement because >

Re: open, close

2019-09-01 Thread MRAB
On 2019-09-01 16:46, Barry wrote: On 31 Aug 2019, at 15:41, Manfred Lotz wrote: When you say COULD this sounds like it is a matter of luck. My thinking was that USUALLY the file will be closed after the statement because then the file handle goes out of scope. It all depends on the way any

Re: open, close

2019-09-01 Thread Manfred Lotz
On Sun, 1 Sep 2019 16:46:44 +0100 Barry wrote: > > On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > > > > When you say COULD this sounds like it is a matter of luck. My > > thinking was that USUALLY the file will be closed after the > > statement because then the file handle goes out of scope.

Re: open, close

2019-09-01 Thread Barry
> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > > When you say COULD this sounds like it is a matter of luck. My thinking > was that USUALLY the file will be closed after the statement because > then the file handle goes out of scope. It all depends on the way any python implementation does

Re: open, close

2019-08-31 Thread Manfred Lotz
On Sat, 31 Aug 2019 16:37:23 +0200 Peter Otten <__pete...@web.de> wrote: > Manfred Lotz wrote: > > > Hi there, > > This is a beginner question. > > > > I learned that > > > > with open("foo.txt") as f: > > lines = f.readlines() > > > > using the with-construct is the recommended way to

Re: open, close

2019-08-31 Thread Manfred Lotz
On Sat, 31 Aug 2019 15:43:41 +0200 Piet van Oostrum wrote: > Max Zettlmeißl writes: > > > On Sat, Aug 31, 2019 at 2:22 PM Manfred Lotz > > wrote: > >> > >> Could I use the latter as a substitute for the with-construct? > >> > > > > You can't use the second statement as a proper substitute

Re: open, close

2019-08-31 Thread Peter Otten
Manfred Lotz wrote: > Hi there, > This is a beginner question. > > I learned that > > with open("foo.txt") as f: > lines = f.readlines() > > using the with-construct is the recommended way to deal with files > making sure that close() always happens. > > However, I also could do: > >

Re: open, close

2019-08-31 Thread Grant Edwards
On 2019-08-31, Manfred Lotz wrote: > Hi there, > This is a beginner question. > > I learned that > > with open("foo.txt") as f: > lines = f.readlines() > > using the with-construct is the recommended way to deal with files > making sure that close() always happens. More importantly, it m

Re: open, close

2019-08-31 Thread Piet van Oostrum
Max Zettlmeißl writes: > On Sat, Aug 31, 2019 at 2:22 PM Manfred Lotz wrote: >> >> Could I use the latter as a substitute for the with-construct? >> > > You can't use the second statement as a proper substitute for the first one. > > With the context manager, it is ensured that the file is close

Re: open, close

2019-08-31 Thread Max Zettlmeißl via Python-list
On Sat, Aug 31, 2019 at 2:22 PM Manfred Lotz wrote: > > Could I use the latter as a substitute for the with-construct? > You can't use the second statement as a proper substitute for the first one. With the context manager, it is ensured that the file is closed. It's more or less equal to a "fin