(Python) How to make python program take a random line from a .txt file?
Question: Hello i am new to python and i would like your help So i just start learning
and the first thing came on my mind is Why not edit a simlpe program ! Well i
want to take a line (a username) and put it in an existing code

Here a part of the code

    
    
    def getRandomUsername():
        username = 'Name_'
        for x in range(0,9):
                username = username + getRandomChar()
        return username
    

I want to pull from my .txt automatically a username and replace it with the
hole thing any ideas ?

As i told im very new to this type of thing also sorry for my bad engls

\--

Thanks Guys


Answer: If your text file is not particularly large, you can read the whole thing into
memory and select a random one:

    
    
    import random
    
    # You can do this once in your program
    lines = open('filename').read().splitlines()
    
    # Whenever you need a new random line, you do this
    randomline = random.choice(lines)
    

