M
M
mypkot32020-01-24 23:34:27
Python
mypkot3, 2020-01-24 23:34:27

How to execute another piece of python code?

python 3.8
code

import telebot
import requests
#качаем нужный файл
def download(fileName):
    f = open(fileName,'wb')
    f.write(requests.get('site.com', headers={'User-Agent': 'My User Agent 1.0'}).content)
    f.close()
for i in range(1):
    download(str(i)+'.jpg')

#отправляем скачаный файл пользователю при нажатии start
bot = telebot.TeleBot('token')
@bot.message_handler(commands=['start'])	
def start_message(message):
      chat_id=id
      bot.send_photo(chat_id=chat_id, photo=open('0.jpg', 'rb'))
    

bot.polling()

How can I repeat the section of code after clicking on start so that it performs the download again
#качаем нужный файл
def download(fileName):
    f = open(fileName,'wb')
    f.write(requests.get('site.com', headers={'User-Agent': 'My User Agent 1.0'}).content)
    f.close()
for i in range(1):
    download(str(i)+'.jpg')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mypkot3, 2020-01-25
@mypkot3

The solution was suggested by the user in the comments Andrey call the download function in the start_message function

C
che_aa, 2020-01-25
@che_aa

def download(fileName):
    f = open(fileName,'wb')
    f.write(requests.get('site.com', headers={'User-Agent': 'My User Agent 1.0'}).content)
    f.close()
while True
    download(str(i)+'.jpg')

to download endless times
def download(fileName):
    f = open(fileName,'wb')
    f.write(requests.get('site.com', headers={'User-Agent': 'My User Agent 1.0'}).content)
    f.close()
for i in range(100):
    download(str(i)+'.jpg')

for downloading 100 times, just change the number in the range brackets, but in general it’s better to learn the language

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question