On 2007-11-28, Joseph king <[EMAIL PROTECTED]> wrote: > Hey i was wondering if any one would know if there was a way to > have python randomly read form a file or would you ahve to know > the byte postion and somehow randomize splicing the file so the > sentence you want show's up. > > i.e have a file with a lot of tips and useless facts and then > have python randomly read one sentence when the code is run and > print at any time you would want it to.
I use a Python application sort of like that to install the random quotes in my sig. It reads a text file containing one quote per line, selects a random one, and updates my signature file with a quote half the time. Originally it ran as a daemon and updated my sig every 60 seconds, but it was a pain to remember to stop and start it. Now I just have the program run automatically whenever I edit a post. Incidentally, you don't want to the the C++ program that this replaced. import textwrap import random import os print "Sigswap v0.4" homedir = os.getenv("HOME") homedir = '/'.join(homedir.split('\\')) sigpath = homedir + '/my.sig' quotepath = homedir + '/quotes.txt' qfile = file(quotepath) quotelist = [] for quote in qfile: quotelist.append(quote) qfile.close() sigfile = file(sigpath, "w") sigfile.write("-- \n") sigfile.write("Neil Cerutti\n") random.seed() if random.choice([True, False]): quote = random.choice(quotelist) for line in textwrap.wrap(quote, 78): sigfile.write(line) sigfile.write('\n') sigfile.close() -- Neil Cerutti Cyrus McCormick invented the McCormick Raper, which did the work of a hundred men. --History Exam Blooper -- http://mail.python.org/mailman/listinfo/python-list