[Tutor] read line x from a file

Orri Ganel singingxduck at gmail.com
Sat Jan 22 05:52:32 CET 2005


Jay Loden wrote:

>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
>>    
>>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
Whoops! Sorta took the hard way on that one. *readlines()* is by all 
means a much better way of doing this.

-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050121/a81e715c/attachment-0001.htm


More information about the Tutor mailing list