On 11/11/2018 06:49, Asad wrote:
> Hi All ,
> 
>          If I am loading a logfile what should I use from the option 1,2,3
> 
> f3 = open ( r"/a/b/c/d/test/test_2814__2018_10_05_12_12_45/logA.log", 'r' )
> 
> 1) should only iterate over f3

This is best for processing line by line which is the most
common way to handle files. It saves memory and allows you
to exit early, without reading the entire file if you are
only looking for say a single entry.

for line in file:
   if terminal_Condition: break
   # process line here

> 2) st = f3.read()

The best solution if you want to process individual characters
or small character groups. Also best if you want to process
the entire file at once, for example using a regular expression
which might span lines.

> 3) st1 = f3.readlines()

Mainly historical and superseded by iterating over the file.
But sometimes useful if you need to do multiple passes over
the lines since it only reads the file once. Very heavy
memory footprint for big files.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to