#Import system modules import os, shutil File2Send = "V:\\GIS\\Maps\\County\\11x17shd" #New maps folder FTP_Server = "F:\\ftp_server\\GIS_Maps" #FTP server folder #Get the list of files f = [f for f in os.listdir(File2Send) if os.path.isfile(os.path.join(File2Send, f))] #Loop through the list for i in (f): if os.access(FTP_Server + "\\" + i, os.F_OK): #Check for existing file os.remove(FTP_Server + "\\" + i) #Remove existing file shutil.copy(File2Send + "\\" + i, FTP_Server + "\\" + i) #Copy new file
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()
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()
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.
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()
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		# Written by Jesusa M. Romero on 06/10/10.
# Script Description:
# 1) Deletes the existing folders AND files on FTP
# 2) Lists folders on rcant7 and creates the same list of folders on FTP
# 3) Transfers TIF images from rcant7 to the corresponding folders on FTP
# 4) Deletes the list of folders on rcant7
# Note: The "notes.txt" file on rcant7 isn't transferred or deleted.
# ---------------------------------------------------------------------------#
import ftplib, os, shutil, sys
startpath = r"\\rcant7\data\MAPPING\GIS\Images"
ftp = ftplib.FTP('IP ADDRESS GOES HERE')
ftp.login('USERNAME', 'PASSWORD')
directory = '/ACR_Images/'
ftp.cwd(directory)
print "Script: Image transfer from Rcant7 to Survey FTP now executing..."
print " "
ftpdirset = ftp.nlst(directory)
for ftpd in ftpdirset:
    dirfold = directory + ftpd
    print "Visiting existing FTP folders/files: "  + dirfold
    try:
        ftp.delete(dirfold)
        print "FTP file deleted: " + dirfold
        print "-" * 70
    except:        
        ftpfiles = ftp.nlst(dirfold)
        for ftpfile in ftpfiles:
            dirfoldfile = dirfold + "/" + ftpfile
            print "     File in folder deleted: " + dirfoldfile
            ftp.delete(dirfoldfile)
        ftp.rmd(dirfold)
        print "FTP folder deleted: " + dirfold
        print "-" * 70
print "-" * 70
print "FTP directory " + directory + " has been emptied."
print "Images will be transferred."
print "-" * 70
print "-" * 70
print " "
dirListST = os.listdir(startpath)
for folder in dirListST:
    if os.path.isdir(startpath + "\\"+ folder):    
        print "Creating new " + folder + " folder in FTP " +  directory
        print "Transferring files to FTP now..."
        ftp.cwd(directory)
        ftp.mkd(folder)
        stpathfolder = os.path.join(startpath, folder)
        dirListST2 = os.listdir(stpathfolder)
        for specfile in dirListST2:                
            basename, extension = specfile.split(".")
            pathforspecfile = stpathfolder + "\\" + specfile
            ftp1 = startpath + "\\"+ folder + "\\" + specfile
            ftp.cwd(directory + folder + "/")
            if extension == "tif" or extension == "txt":
                f = open(ftp1,"rb")
                a = "STOR " + specfile
                ftp.storbinary (a, f)
                print "     Transfer completed for " + specfile
                f.close()
            else:
                print "     File not transferred: " + specfile
        print stpathfolder + " has been deleted."
        shutil.rmtree(stpathfolder)
    else:
        print "*** " + folder + " is not a directory. Not transferred from Rcant7."
    print "-" * 70
print "-" * 70
print "IMAGE TRANSFER HAS BEEN COMPLETED."
                              
ftp.quit()
If you want to discuss this further, just post your e-mail. 🙂
import ftplib
import os
import sys
import traceback
print "Logging in..."
ftp = ftplib.FTP()
ftp.connect('ftphostname')
print ftp.getwelcome()
try:
    try:
        ftp.login('username', 'password')
        ftp.cwd('target directory')
        # move to the desired upload directory
        print "Currently in:", ftp.pwd()
        print "Uploading...",
        fullname = '../sourcedirectoy/filename'
        name = os.path.split(fullname)[1]
        f = open(fullname, "rb")
        ftp.storbinary('STOR ' + name, f)
        f.close()
        print "OK"
        
        print "Files:"
        print ftp.retrlines('LIST')
    finally:
        print "Quitting..."
        ftp.quit()
except:
    traceback.print_exc()