Send html file to FTP - Python 3.6

Asked

Viewed 374 times

0

Forgive me for asking the layman, but I’ve searched everywhere without finding a way out. I have the code below and I am trying to send the files directly to FTP (initially I wanted to do this without having to save locally, but due to the difficulty in doing I am leaving for another idea). This code is working perfectly until you save the files locally but from the ftp.storlines gives the following error: "Typeerror: a bytes-like Object is required, not 'str'". Can anyone help?

from dominate import document
from dominate.tags import *
from ftplib import FTP
import getpass

psw = getpass.getpass('Digite sua senha: ')
ftp = FTP('sasasa.com.as')
ftp.login('usr', psw)
ftp.cwd('sasasa.com.as/cdc/sds')

border = "1"

for index, row in UltimaCotacao.iterrows():
    h = html()
    with h.add(body()):
        h1('Algo')
        with table().add(tbody()):
                l = tr()
                l += td('CNPJ: ', row['CNPJ_'])
                l += td('Valor da cota: ', row['VL_QUOTA'])
                l += td('Data: ', row['DT_COMPTC'])
    g = format_filename(row['CNPJ_'])+'.html'
    with open(g, 'w') as f:
        f.write(h.render())
    locpath = 'C:/Users/proj/'
    ftp.storlines('STOR ' + g, open(locpath+g, 'r'))
ftp.quit()

I’m sorry if the question is unclear, but as I said, I’m a total layman in any programming language (I started a month ago out of curiosity and fascination). Hugs,

1 answer

0

I managed to solve with a little more research. I understood that we need to create the file before sending it (I thought this was already being done here f.write(h.render()) but somehow it wasn’t. I would also have to open it as binary. So I created a temporary file to upload it. It was like this:

g = format_filename(row['CNPJ_'])+'.html'
#with open(g, 'w') as f:
    #f.write(h.render())
#locpath = 'C:/Users/proj/'
arqtemp = open('temp.html', 'w').write(h.render())
ftp.storlines('STOR ' + g, open('arqtemp', 'rb'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.