samedi 13 octobre 2018

Improving a code for choosing a name from a file list of names

I'm new here.

I'm working in a code in python and need to chose a random name. I do have a text file with thousands of names (names.txt). Each name is in one line. I want to create a code to choose a name from this list. I've have already did this:

file_names = open('names.txt', 'r')

names = []
for name in file_names.readlines():
    names.append(name.replace('\n', ''))

random_name = random.choice(names)

I want to know if there's anyway to do this better.




Aucun commentaire:

Enregistrer un commentaire