[Tutor] Reading a file

David Hutto smokefloat at gmail.com
Fri Jul 2 18:57:28 CEST 2010

On Fri, Jul 2, 2010 at 11:31 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, 2 Jul 2010 11:16:02 pm David Hutto wrote:
>> In the code below, I'm trying to read from a file, and print out the
>> lines.
> f = open("filename.txt")
> for line in f:
>    print line
> f.close()
> is all you need, and even the close at the end is optional (but
> recommended).
>> I know I've seen the exact function I'm looking for here, but
>> it seems like the below 'should' work.
>> The second function(rdfil) works, if just the line number is placed
>> there, and th first(increment) works for printing a new number in
>> range for the rdfil to operate while going through the lines in the
>> file.
>> Any other options other than using linecache.getline(I'm sure there
>> are, just haven't jogged the right memory cell loose yet)?
>> def increment():
>>       for number in range(1, 500):
>>               print number ++1
> Why do you have number ++1 instead of just number+1 ?
> For that matter, why do you have number+1 instead of changing the range
> to range(2, 501)?

I was 'thinking' that since it worked in the interpreter as printing
the numbers 1-500 , it would cycle through to the linecache.getline's
number of line variable that it calls when it went back through each
time. It might be better explained as to what I thought it was doing.

>> def rdfil():
>>       outputfile = raw_input('Input file name: ')
> This puts a *string* in the variable outputfile. A better name would
> be "outputfilename", because it is a file NAME, not a file.
>>       for line in outputfile:
> This is terribly misleading, because iterating over a string gives you
> the individual characters of the string, not lines. E.g.:>
> for line in "file.txt":
>    print line
> will print:
> f
> i
> l
> e
> .
> t
> x
> t
>>               if line:
> This will always be true, and so is pointless. Even an empty line is not
> truly empty, as it will be a newline character. When the file is empty,
> the for-loop will exit.

This what I thought would happen for the each line, and it would only
loop through until it hit a 'non-assigned' line, which would stop the
call of the for range function, and stop telling the getline to spit
out a new number in range.

Here is where using the above worked and why I added it to the function:

david at david-laptop:~/Documents/inprogress/projectsmain/Python Projects$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import linecache
>>> readcont = linecache.getline('/home/david/testmkfile.txt', 1)
>>> print readcont


the for line in was added after to the function in an app(which is
where it's being called from) , and I assumed it would go through the
file line by line like that. I could have checked the function better
before placing it to be called by the app.

>>                       readcont = linecache.getline(outputfile, increment) '''increment
>> is originally supposed to be an int for the line number being read'''
> This line gives a syntax error. What is it supposed to be? What is the
> purpose of the string? It looks like it is meant to be a comment, but
> why have you written it as a string instead of a # comment?

I started with the #, it was added at the time of the email to explain
why the variable was there and not a numerical line number. At the
last second I added it because I thought if you copied and pasted it,
it would consume more than a one line comment for the person copying
due to the wordwrap.

> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Thanks, for the memory jog, and snippet though.


More information about the Tutor mailing list