Request for Enhancement

Steve Holden sholden at holdenweb.com
Wed Aug 30 22:09:30 EDT 2000


"Samuel A. Falvo II" wrote:
> 
> I have need to process very large text files in Python, but I don't have any
> idea how long the files are going to be in real-world situations.  It is
> unfortunate that there is no F.eof() function, where F is a Python file.
> 
> Here's what I *want*:
> 
>         while not F.eof():
>                 l = F.readline()
>                 ...process line...
> 
> As it is, I have to do the following:
> 
>         l_list = F.readlines()  #note plural
>         for line in l_list:
>                 ... process line ...
> 
> While this is fine for my test cases, it could consume unacceptable amounts
> of memory when fed large text files.
> 
Samuel:

The classic pythonic method of processing such files is:

while 1:			# loop forever
    line = F.readline		# get the next line
    if line == "": break	# null string indicates EOF
    ... process line ...

It often seems unnatural to Python newcomers, but you get used to it!

regards
 Steve

> Thanks.
> 
> --
> KC5TJA/6, DM13, QRP-L #1447 | Official Channel Saint, *Team Amiga*
> Samuel A. Falvo II          |
> Oceanside, CA               |
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/



More information about the Python-list mailing list