Uploading PDF through FTP using Python

7874
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
2 Solutions

Accepted Solutions
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

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?

View solution in original post

21 Replies
DanPatterson
MVP Esteemed Contributor

 filepath = open(r'G:\\GIS\\filepath\\TEST12142020.pdf', 'rb')  

You made this a file object.

Did you mean

 filepath = r'G:\\GIS\\filepath'

and the previous to be something like

file_object = open(r'G:\\GIS\\filepath\\TEST12142020.pdf', 'rb')  

Then decide what actually needs to go in here

 session.storbinary('STOR ' + ???, ????)

 


... sort of retired...
0 Kudos
Bubacz_Hannah
Emerging Contributor

Hey Dan, so like this? I get the same error.

 

filepath = r'G:\\GIS\\filepath'
file_object = open(r'G:\\GIS\\filepath\\TEST12142020.pdf', 'rb')
print ('Opened file')

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

0 Kudos
Bubacz_Hannah
Emerging Contributor

I also get the same error doing this:

 

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

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

0 Kudos
BlakeTerhune
MVP Regular Contributor

As the error says, you cannot concatenate 'str' and 'file' objects. You'd have to do something like

filepath = r'G:\\GIS\\filepath'
file_object = open(r'G:\\GIS\\filepath\\TEST12142020.pdf', 'rb')
print ('Opened file')

# Sending the file
print('Sending file')
session.storbinary('STOR ' + filepath, file_object)
print('Sent file')
0 Kudos
Bubacz_Hannah
Emerging Contributor

Ah, I didn't realize that filepath and file_object were actualy variable names that it would recognize, not just what I chose to call it. I am running the code through Notepad++ with Run on Python27 and got these errors:

Logging in
Opened file
Sending file
Traceback (most recent call last):
File "G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\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: G:\\GIS\\Departments\\ItmdGis\\Users\\hbubac\\Data and Scripts\\Crime Maps\\new scripts

0 Kudos
BlakeTerhune
MVP Regular Contributor

You formatted the file path a a raw string but then included the escape backslash. You have to do one or the other. I recommend just using the raw string with single backslashes.

filepath = r'G:\GIS\filepath'
file_object = open(r'G:\GIS\\filepath\TEST12142020.pdf', 'rb')
print ('Opened file')

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

 

0 Kudos
Bubacz_Hannah
Emerging Contributor

Ah, my bad. You are right. I changed it to this:

filepath = r'G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts'
file_object = open(r'G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\TEST12142020.pdf', 'rb')
print ('Opened file')

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

 

And still got this for an error:

Logging in
Opened file
Sending file
Traceback (most recent call last):
File "G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\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: G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts

0 Kudos
BlakeTerhune
MVP Regular Contributor

The command input needs to be a path to the file, not a folder. Try

filepath = r'G:\GIS\filepath\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')
0 Kudos
Bubacz_Hannah
Emerging Contributor

Hmmm, looks like the same error. Maybe I need to run it some way different than Python 27? Or my Python version is getting mixed up because I have both ArcMap and ArcPro installed on my computer?

CODE:

# Choosing file to send
filepath = r'G:\GIS\filepath\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 "G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\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: G:\GIS\Departments\ItmdGis\Users\hbubac\Data and Scripts\Crime Maps\new scripts\TEST12142020.pdf

0 Kudos