.HTML files won't send with ftplib storebinary

49 Views Asked by At

I am unable to use ftp.storebinary to send .HTML files. HTML files are the only files I have run into so far. I can send all other files I have tried including but not limited to, .txt, .js , .jpg, .bmp ...

Operating system and architecture: Windows 11, Python 3.9.7

    import ftplib
    ftp = ftplib.FTP("ftpupload.net") # FTP URL
    ftp.login("username", "password") # FTP ([Username],[Password])
    ftpPath = '/test/'
    ftp.cwd(ftpPath) # FTP directory

    ftp.storbinary('STOR ' + 'index.html', 
    open(r'C:\Users\whybo\Desktop\index.html', 'rb'))
2

There are 2 best solutions below

2
tmc On

Transfering file using ftp.storbinary() can get issues if your .html file containing non-ASCII characters. Thus, your .html file might contain non-ASCII characters, which are harmful and prevent you from sending the file.

You can use ftp.storlines() method which operates on ASCII mode and it can handle files containing non-ASCII characters like .html files.

Last two lines of your code can be replaced by:

file_name = 'index.html'
with open(r'C:\Users\whybo\Desktop\index.html', 'rb') as f:
    ftp.storlines(f'STOR {file_name}')
ftp.quit()    # to close the ftp connection
3
Fin Elliott On

My FTP provider will not allow me to install .HTML files outside of the designated location.. Problem Solved!