S
S
Starshovv2021-02-05 10:31:10
Python
Starshovv, 2021-02-05 10:31:10

How to make a Python script to insert a random line from a txt file anywhere?

We need a script that, when run, would insert a random line from a txt file to a given location. That is, the script is assigned to a hot key using a special program (I have such a program, I only need a script), the execution of this script starts and random text from the txt file is inserted into any place - a field on the site, a text editor, and so on. You can not random, but sequential. Those. at the first insertion the 1st row, at the second the 2nd, it will be even better.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MinTnt, 2021-02-05
@Starshovv

Here's roughly:

import random

File_text = open('file.txt', 'r').readlines()
random_text = random.choice(File_text)

A small explanation:
.readlines() - loads the text of the file line by line, as a list
random.choice() - select a random element from the list
And then use keyautogui to make a hot key and insert text

S
Seo5, 2021-07-23
@Seo5

More correct https://saitsozdanie.ru/forum/index.php?topic=8986.0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question