Print random line from txt file?
<p>I'm using random.randint to generate a random number, and then assigning that number to a variable. Then I want to print the line with the number I assigned to the variable, but I keep getting the error:</p>

<blockquote>
  <p>list index out of range</p>
</blockquote>

<p>Here's what I tried:</p>

<pre><code>f = open(filename. txt)
lines = f.readlines() 

rand_line = random. randint(1,10)
print lines[rand_line]
</code></pre>

<p>You want to use <code>random.choice</code></p>

<pre><code>import random
with open(filename) as f:
    lines = f.readlines()
    print(random.choice(lines))
</code></pre>

python random