[Tutor] Equality Check.
Alan Gauld
alan.gauld at btinternet.com
Mon Mar 3 00:44:04 CET 2014
On 02/03/14 21:10, Tyler Simko wrote:
> So I called readlines() on a file, and I'm wondering how
> I can check the equality of a specific line with a raw_input
> file = open('filename.txt,' 'r')
> file.readlines()
This opens the file and reads all the lines but it
doesn't store the result anywhere so you just throw
those lines away.
The recommended way to do this in Python nowadays is:
with open('filename.txt') as infile:
for line in infile:
# process line here
> variable_name = raw_input()
> if file[2] == variable_name:
file is a file object you cannot index into it using [2].
But even if you had used readlines and stored the result
checking line by line as shown above is probably a
better approach.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list