'Read each line of file but return empty string in python

I wanna build a list with some random lines from another archive. In my archive I have some questions and the list must contain random lines of it (for create a document with that questions)

My problem appears at time to check if all it's OK, My code runs but I dont know why some entries of my list are empty numLineasFichero is a function that returns a int with the lines of an archive.

def numLineasFichero(fichero): 
    fichero.seek(0)
    return len(fichero.readlines())

The actual content of archivoPreguntas

Como te llamas?
Cuantos años tienes?
Donde vives?
Como tomas apuntes?
def gen_Preg():
    preguntas = []                     # Creamos una lista vacia para almacenar las preguntas

    # Bucle for para obtener y escribir
    with open("archivoDePreguntas", 'r') as aPreg:
        for i in range(0,6):
            preguntas.append(linecache.getline('archivoDePreguntas',random.randint(0,numLineasFichero(aPreg))))
    print(preguntas)

This should generate 6 entries in list, but one result of entire list was this.

['Donde vives?\n', 'Como te llamas?\n', 'Cuantos años tienes?\n', 'Donde vives?\n', 'Cuantos años tienes?\n', '']

The problem was located in empty entry at the end, but this was only one of the results. I think that it comes because I use random as numberline that I gonna include in the list.

Other results:

['', 'Como te llamas?\n', 'Donde vives?\n', 'Como tomas apuntes?\n', 'Donde vives?\n', 'Como tomas apuntes?\n']
['Como tomas apuntes?\n', 'Como te llamas?\n', 'Como tomas apuntes?\n', '', 'Como tomas apuntes?\n', 'Cuantos años tienes?\n']
['Como tomas apuntes?\n', 'Donde vives?\n', 'Cuantos años tienes?\n', 'Cuantos años tienes?\n', 'Como tomas apuntes?\n', '']

I need only questions not empty entries, how I can fix it?

NOTE: Some entries are replicate because I asked for 6 different questions, but right know I only include 4 questions for some tests. I must abord then the problem of duplicate entries, but the problem that I need help is with empty entries



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source