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>

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

rand_line = random.randint(0, (len(lines) - 1))  # https://docs.python.org/2/library/random.html#random.randint
print lines[rand_line]
</code></pre>

python random