Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

333
Vistas
supply a filename for a file-like object created by urlopen() or requests.get()

I am building a Telegram bot using a Telepot library. To send a picture downloaded from Internet I have to use sendPhoto method, which accepts a file-like object.

Looking through the docs I found this advice:

If the file-like object is obtained by urlopen(), you most likely have to supply a filename because Telegram servers require to know the file extension.

So the question is, if I get my filelike object by opening it with requests.get and wrapping with BytesIO like so:

res = requests.get(some_url)
tbot.sendPhoto(
    messenger_id,
    io.BytesIO(res.content)
)

how and where do I supply a filename?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You would supply the filename as the object's .name attribute.

Opening a file with open() has a .name attribute.

>>> local_file = open("file.txt")
>>> local_file
<open file 'file.txt', mode 'r' at ADDRESS>
>>> local_file.name
'file.txt'

Where opening a url does not. Which is why the documentation specifically mentions this.

>>> import urllib
>>> url_file = urllib.open("http://example.com/index.html")
>>> url_file
<addinfourl at 44 whose fp = <open file 'nul', mode 'rb' at ADDRESS>>
>>> url_file.name
AttributeError: addinfourl instance has no attribute 'name'

In your case, you would need to create the file-like object, and give it a .name attribute:

res = requests.get(some_url)
the_file = io.BytesIO(res.content)
the_file.name = 'file.image'

tbot.sendPhoto(
    messenger_id,
    the_file
)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda