Uploading PDF through FTP using Python

812
21
Jump to solution
03-04-2021 08:53 AM
Bubacz_Hannah
New Contributor III

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

Try putting the file in a simpler directory, like C:\temp\TEST12142020.pdf

0 Kudos
Bubacz_Hannah
New Contributor III

CODE:

# Choosing file to send
filepath = r'C:\temp\TEST12142020.pdf'
file_object = open(filepath, 'rb')
print ('Opened file')

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

ERROR:

Logging in
Opened file
Sending file
Traceback (most recent call last):
File "C:\temp\UploadtoFTPTest.py", line 14, in <module>
session.storbinary('STOR ' + filepath, file_object)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 471, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 553 Prohibited file name: C:\temp\TEST12142020.pdf

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

According to this, try just using the file name in the command.

# Choosing file to send
filename = 'TEST12142020.pdf'
filepath = r'C:\temp\{}'.format(filename)
file_object = open(filepath, 'rb')
print ('Opened file')

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

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

The "filename" is the name you want on the FTP Server, not the name of the file on the local machine.  The local file is already addressed through the file object being passed to the command.

Bubacz_Hannah
New Contributor III

That worked! Perfect, thank you. I still have some finagling but this is definitely one step closer. If you don't mind, one more question. It uploaded the file but then I got an error about my closing of the file. I tried closing filepath, file_object, and filename and all got the error saying it received a string.

# Choosing file to send
filename = 'TEST12142020.pdf'
filepath = r'C:\temp\{}'.format(filename)
file_object = open(filepath, 'rb')
print ('Opened file')

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

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

ERROR:

Logging in
Opened file
Sending file
Sent file
Traceback (most recent call last):
File "C:\temp\UploadtoFTPTest.py", line 19, in <module>
file.close(filepath)
TypeError: descriptor 'close' requires a 'file' object but received a 'str'

0 Kudos
BlakeTerhune
MVP Regular Contributor

Don't pass anything as an argument. Just do file_object.close()

However, a better way would be to use the with keyword to open and automatically close the file. Otherwise, you should be using a try/except/finally with open/close.

# define ftp session out here
try:
    # Choosing file to send
    filename = 'TEST12142020.pdf'
    filepath = r'C:\temp\{}'.format(filename)
    with open(filepath, 'rb') as file:
        print ('Opened file')

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

finally:
    session.quit()
Bubacz_Hannah
New Contributor III

Perfect. Thank you so much for all your help.

0 Kudos
Bubacz_Hannah
New Contributor III

Hello,

Could you help me with one more thing? I need to put it in a certain folder within the ftp site but cannot figure out the code. Thoughts? Thanks!

The folder I need it in:

Bubacz_Hannah_0-1615300876257.png

 

CODE:

# Sending the file
print('Sending file')
session.storbinary('STOR {/srv/ftp/pub/gis/crimemaps}' + filename, file_object)
print('Sent file')

ERROR:

<module>
session.storbinary('STOR {ftp.milwaukee.gov/srv/ftp/pub/gis/crimemaps}' + filename, file_object)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 471, in storbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 553 Can't open that file: No such file or directory

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Change the working directory before you call the STOR command to copy the file.

 

session.cwd('/srv/ftp/pub/crimemaps')
# Sending the file
print('Sending file')
session.storbinary('STOR ' + filename, file_object)
print('Sent file')

 

0 Kudos
Bubacz_Hannah
New Contributor III
filename = 'TEST12142020.pdf'
filepath = r'G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\{}'.format(filename)
file_object = open(filepath, 'rb')
print ('Opened file')

# Sending the file
session.cwd('/srv/ftp/pub/crimemaps')
print('Sending file')
session.storbinary('STOR ' + filename, file_object)
print('Sent file')

 

ERROR:

 <module>
session.cwd('/srv/ftp/pub/crimemaps')
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 562, in cwd
return self.voidcmd(cmd)
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 254, in voidcmd
return self.voidresp()
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 229, in voidresp
resp = self.getresp()
File "C:\Python27\ArcGIS10.5\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 550 Can't change directory to /srv/ftp/pub/crimemaps: No such file or directory

 

0 Kudos