E
E
EduUse2020-08-16 23:02:46
Python
EduUse, 2020-08-16 23:02:46

How to write a random string from a .txt file to a variable?

There is a .txt file
. It is necessary to write a random string to a variable, how to implement it in Python. Thanks in advance )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SirotaKazansky, 2020-08-16
@SirotaKazansky

with open('my.txt') as fr:
        lines = fr.readlines()
        random_line = random.choice(lines).strip() if lines else None

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question