Sounds crazy but you have to trust me here. If you are going to use storbinary, the file cant be opened in binary mode. I know what you are going to say but I just figured this out in the last couple weeks myself and it just doesnt work.
fileList=[]
for i in os.listdir(dir):
if i.split('.')[1] == 'pdf' #if it ends in pdf
fileList.append(i) #put it in the list
ftp = FTP("XXX.XXX.XXX.XXX",username, password) #save a line and just put your U:P here.
for i in fileList:
file = open(i, "r") #open in normal read mode
ftp.cwd("//data2//ftp//pub//download//maps//")
ftp.storbinary('STOR %s' % i, os.path.join('V://GIS//Maps//County//11x17shd//',i))
file.close()
ftp.quit()
Your using storbinary wrong:
ftp = FTP("XXX.XXX.XXX.XXX",username, password) #save a line and just put your U:P here.
for i in fileList:
# don't use double //, this will rely on the server to properly handle it (most will)
# also don't use a trailing slash since VAX/RSIC will try the folder "maps/"
ftp.cwd("/data2/ftp/pub/download/maps")
# ftp.storbinary takes a FILE object not a path, python will automatically close the file
ftp.storbinary('STOR %s' % i, open(os.path.join('V://GIS//Maps//County//11x17shd//',i), "rb"))
ftp.quit()
storbinary takes a file like object as the second parameter.If you are transfering ANYTHING but a text file, you need to use binary otherwise you will be faced with data corruption:From Python Documentation:
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it�??ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn�??t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.
There are/can be tons of other little nitpicky issues relating to FTP, is old and was not standardized properly, alot of ftp servers have confilicting behaviour, (some servers use binary as a standard mode, others use A, etc etc). In general if you are going to try and transfer a binary file i suggest you manually change the server's connection type (ftplib is flaky about this):
ftp = FTP("XXX.XXX.XXX.XXX",username, password) #save a line and just put your U:P here.
for i in fileList:
ftp.cwd("/data2/ftp/pub/download/maps")
# Manually tell the server to change to binary mode:
# I indicates Binary,
# A indicates text
# There are a few other transfer modes however they are not used very often
# just print the return message, if its an error ftplib will raise an exception
print ftp.sendcmd("TYPE I")
# ftp.storbinary takes a FILE object not a path, python will automatically close the file
ftp.storbinary('STOR %s' % i, open(os.path.join('V://GIS//Maps//County//11x17shd//',i), "rb"))
ftp.quit()