Re: [Tutor] reading lines from a list of files

2015-05-14 Thread Laura Creighton
In a message of Wed, 13 May 2015 22:27:11 -0700, Alex Kleider writes: >As a follow up question: >The following seems to work- > > for f_name in list_of_file_names: > for line in open(f_name, 'r'): > process(line) > >but should I be worried that the file doesn't get explicitl

Re: [Tutor] reading lines from a list of files

2015-05-14 Thread Alex Kleider
On 2015-05-13 23:24, Danny Yoo wrote: As a follow up question: The following seems to work- for f_name in list_of_file_names: for line in open(f_name, 'r'): process(line) but should I be worried that the file doesn't get explicitly closed? It depends on context. Pers

Re: [Tutor] reading lines from a list of files

2015-05-14 Thread Alex Kleider
On 2015-05-14 00:01, Alan Gauld wrote: On 14/05/15 06:27, Alex Kleider wrote: The following seems to work- for f_name in list_of_file_names: for line in open(f_name, 'r'): process(line) but should I be worried that the file doesn't get explicitly closed? If you ar

Re: [Tutor] reading lines from a list of files

2015-05-14 Thread Alex Kleider
On 2015-05-14 00:15, Laura Creighton wrote: In a message of Wed, 13 May 2015 22:27:11 -0700, Alex Kleider writes: As a follow up question: The following seems to work- for f_name in list_of_file_names: for line in open(f_name, 'r'): process(line) but should I be worried

Re: [Tutor] reading lines from a list of files

2015-05-14 Thread Alan Gauld
On 14/05/15 06:27, Alex Kleider wrote: The following seems to work- for f_name in list_of_file_names: for line in open(f_name, 'r'): process(line) but should I be worried that the file doesn't get explicitly closed? If you are only ever reading from the file then n

Re: [Tutor] reading lines from a list of files

2015-05-13 Thread Danny Yoo
> As a follow up question: > The following seems to work- > > for f_name in list_of_file_names: > for line in open(f_name, 'r'): > process(line) > > but should I be worried that the file doesn't get explicitly closed? It depends on context. Personally, I'd write it with t

Re: [Tutor] reading lines from a list of files

2015-05-13 Thread Alex Kleider
On 2015-05-11 23:48, Peter Otten wrote: Alex Kleider wrote: Is there a better (more 'Pythonic') way to do the following? for f_name in f_names: with open(f_name, 'r') as f: for line in f: There's the fileinput module

Re: [Tutor] reading lines from a list of files

2015-05-13 Thread Oscar Benjamin
On 12 May 2015 at 19:08, Peter Otten <__pete...@web.de> wrote: >> >> A generator cannot guarantee that execution continues after a yield so >> any context manager used around a yield is dependent on __del__. I >> think a good rule of thumb is "don't yield from a with block". > > Uh-oh, I am afraid

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Albert-Jan Roskam via Tutor
-- On Tue, May 12, 2015 9:54 PM CEST Peter Otten wrote: >Albert-Jan Roskam wrote: > >> It was not that long ago that I found out about the fileinput module, so I >> sometimes forget to use it. It is not specify the encoding of the files, >> is it? It'd be nice if one c

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Albert-Jan Roskam wrote: > It was not that long ago that I found out about the fileinput module, so I > sometimes forget to use it. It is not specify the encoding of the files, > is it? It'd be nice if one could specify a tuple of encodings, e.g. > ('utf-8-sig', 'latin1', 'cp1252'). > > input(fil

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Albert-Jan Roskam via Tutor
-- On Tue, May 12, 2015 8:48 AM CEST Peter Otten wrote: >Alex Kleider wrote: > >> Is there a better (more 'Pythonic') way to do the following? >> >> for f_name in f_names: >> with open(f_name, 'r') as f: >> for line in f: > >There's the file

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Oscar Benjamin wrote: > A generator cannot guarantee that execution continues after a yield so > any context manager used around a yield is dependent on __del__. I > think a good rule of thumb is "don't yield from a with block". Uh-oh, I am afraid I did this quite a few times. Most instances seem

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread David Rock
* Alex Kleider [2015-05-12 02:33]: > On 2015-05-11 23:48, Peter Otten wrote: > > Alex Kleider wrote: > > > >> Is there a better (more 'Pythonic') way to do the following? > >> > >> for f_name in f_names: > >> with open(f_name, 'r') as f: > >> for line in f: > > > > Th

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Oscar Benjamin
On 12 May 2015 at 11:46, Peter Otten <__pete...@web.de> wrote: > > > although the reason > > for my inquiry was more to diminish levels of indentation > > than number of lines. > > You usually do that by factoring out the loops into a generator: > > def lines(files): > for file in files: >

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Alex Kleider wrote: > On 2015-05-11 23:48, Peter Otten wrote: >> Alex Kleider wrote: >> >>> Is there a better (more 'Pythonic') way to do the following? >>> >>> for f_name in f_names: >>> with open(f_name, 'r') as f: >>> for line in f: >> >> There's the fileinput modu

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Alex Kleider
On 2015-05-11 23:48, Peter Otten wrote: Alex Kleider wrote: Is there a better (more 'Pythonic') way to do the following? for f_name in f_names: with open(f_name, 'r') as f: for line in f: There's the fileinput module

Re: [Tutor] reading lines from a list of files

2015-05-11 Thread Peter Otten
Alex Kleider wrote: > Is there a better (more 'Pythonic') way to do the following? > > for f_name in f_names: > with open(f_name, 'r') as f: > for line in f: There's the fileinput module but persona

[Tutor] reading lines from a list of files

2015-05-11 Thread Alex Kleider
Is there a better (more 'Pythonic') way to do the following? for f_name in f_names: with open(f_name, 'r') as f: for line in f: As always, thank you, tutors, for all you are doing. AlexK ___ Tutor maillist - Tutor@python.org