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
Solved! Go to Solution.
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')
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?
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 ' + ???, ????)
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')
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')
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')
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
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')
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
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')
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