Send html file to FTP - Python 3.6

0

I'm sorry for the layman's question but I've researched everywhere without finding a way out. I have the code below and I'm 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'm starting for another idea). This code is working fine until you save the files locally but from 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()

Sorry if the question is not clear but is, as I said, I am a total layman in any programming language (I started a month ago out of curiosity and fascination). Hugs,

    
asked by anonymous 07.12.2017 / 18:59

1 answer

0

I was able to solve it with a little more research. I understand that I need to create the file before I send it (I thought this was already being done here f.write(h.render()) but somehow it was not.I would also have to open it as a binary.Then I created a temporary file to send it. It looks 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'))
    
10.12.2017 / 15:27