[Tutor] read line x from a file

Jay Loden python at jayloden.com
Sat Jan 22 05:46:41 CET 2005


One simple solution is to do: 

fle = open(file)
contents = file.readlines()
file.close()
print contents[x]  #or store this in a variable, whatever

-Jay

On Friday 21 January 2005 11:22, J. M. Strother wrote:
> I have  a text file containing 336 records.
> I can read and print out the whole file without any problem.
> What I want to do is read and print out one record only (chosen at
> random). So I need to get to record x, select it, and then print it (or
> store it in a variable).
> Can anyone tell me now to do this?
>
> I'm new to Python and programming, so sorry if this is very basic. Thanks.
>
> jon
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list