Opening random line from file with random.shuffle()

Duncan Smith buzzard at urubu.freeserve.co.uk
Wed Feb 13 21:09:42 EST 2002


"synthespian" <synthespian at uol.com.br> wrote in message
news:87bses6g1p.fsf at uol.com.br...
>
>
> Hello-
>
> I need to get a random line from a file. I read in thins NG about a
"poorly" documented random.shuffle(), but I can't get it to work:
>
>
>
>
> #!/usr/local/bin/python2.0
>
> import random
> file = open('/xxxx/yyy/zzz.txt')
>
> allLines = file.readlines()
>
> file.close()
>
> getLine = random.shuffle(allLines)
>

Shuffles it in place and returns None.  Try,

getLine = random.choice(allLines)


> print getLine,
> string = str(getLine)
>
> print string,
>
>
> All I get as output is
> "None None"
>
> Thanks for __any__ help!
>
> Regs
> Henry
>
>
>
>
>
>
> ----------------------
> _________   1001101010
> | ^   ^ |  /1111100110
> | o   o | / 0010100111
> |   v |/  1110100111
> | ----- |
> \_______/
>
> ______________________
> Micro$oft-Free Human
> 100% Debian GNU/Linux
> ----------------------
> KMFMS
>





More information about the Python-list mailing list