Uploading PDF through FTP using Python

7578
21
Jump to solution
03-04-2021 08:53 AM
Bubacz_Hannah
Emerging Contributor

Hello, I am trying to upload a PDF using Python to an outside FTP server. However, I keep getting the error about concatenating string and and file objects.

CODE:

    import ftplib

    # Logging into ftp site with login information
    session = ftplib.FTP("host","username","password")
    print('Logging in')

    # Choosing file to send
    filename = "TEST12142020.pdf"
    filepath = open(r'G:\\GIS\\filepath\\TEST12142020.pdf', 'rb')
    print ('Opened file')

    # Sending the file
    print('Sending file')
    session.storbinary('STOR ' + filepath, filename)
    print('Sent file')

    # Closing file and ftp
    file.close()
    print('Closing file')
    session.quit()

 

ERROR:

    Logging in
    Opened file
    Sending file
    Traceback (most recent call last):
    File "G:\pathname\UploadtoFTPTest.py", line 14, in <module>
    session.storbinary('STOR ' + filepath, filename)
    TypeError: cannot concatenate 'str' and 'file' objects

Tags (4)
0 Kudos
21 Replies
BlakeTerhune
MVP Regular Contributor

The error is pretty clear: Can't change directory to /srv/ftp/pub/crimemaps: No such file or directory

Can you verify that path is correct and the folders exist in the ftp site?

Bubacz_Hannah
Emerging Contributor

Oops, I had forgotten a folder name in the path. Thank you so much for all your help!!

0 Kudos