ftplib and retrbinary or retrlines (losing newline characters in my log files)

aus stuff austuff at gmail.com
Thu Jan 25 01:05:49 EST 2007


Hi am successfully downloading my text files and writing them to local files
 with either

ftp.retrlines('RETR ' + fl, fileObj.write)"

ftp.retrbinary('RETR <ftp://ftp.retrbinary('retr/> ' + fl, fileObj.write)

However all my recieved (log) files have lost thier newline characters?

Can anyone steer me in the right direction so my 'recieved log' files arent
all jumbled into one line?

(full code below)
any help appreciated :)
(excuse my newbie code copying )

Special thanks to the person/s whom originally made this code (found on
google)


<<<<<<<<<code starts


import string
import os
from sets import Set
from ftplib import FTP
import time

def
copyFTPFiles(serverName,userName,passWord,remotePath,localPath,deleteRemoteFiles=False,onlyDiff=False):
    """Connect to an FTP server and bring down files to a local directory"""

    try:
        ftp = FTP(serverName)
    except:
        print "Couldn't find server"
        #print >>myLogger, "Couldn't find server"
    ftp.login(userName,passWord <ftp://ftp.login(username,password/>)
    ftp.cwd(remotePath <ftp://ftp.cwd(remotepath/>)

    try:

        #print >>myLogger, "Connecting...",
        print "Connecting..."
        if onlyDiff:
            lFileSet = Set(os.listdir(localPath))
            rFileSet = Set(ftp.nlst())
            transferList = list(rFileSet - lFileSet)
            #print >>myLogger,  "Missing: " + str(len(transferList)) + ' >>>
',
            print "Missing: " + str(len(transferList)),
        else:
            transferList = ftp.nlst()
        delMsg = ""
        filesMoved = 0
        for fl in transferList:
            ttt = ''
            ttt = str(fl)
            if string.find(ttt, '.') > 0 :
                print '\nCopying >>> ' + ttt

                # create a full local filepath
                localFile = localPath + fl
                grabFile = True
                if grabFile:
                    #open a the local file
                    fileObj = open(localFile, 'wb')
                    # Download the file a chunk at a time using RETR
                    #ftp.retrbinary('RETR ' + fl, fileObj.write)
                    ftp.retrlines('RETR <ftp://ftp.retrlines('retr/> ' + fl,
fileObj.write)


                    # Close the file
                    fileObj.close()
                    filesMoved += 1

                # Delete the remote file if requested

                if deleteRemoteFiles:
                    ftp.delete(fl)
                    delMsg = " and Deleted"
                    #print >>myLogger,  " and Deleted"

        #print >>myLogger,  "Files Moved" + delMsg + ": " + str(filesMoved)
+ ' >>> ' + " On " + timeStamp()
        print "\nFiles Copied " + delMsg + ": " + str(filesMoved)
    except:
        #print >>myLogger,  "Connection Error - " + timeStamp()
        print "Connection Error - "
    ftp.close() # Close FTP connection
    ftp = None

copyFTPFiles("server","uname","pass","remoteDir","localDir")



>>>>>>>code end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070125/1f3826f6/attachment.html>


More information about the Python-list mailing list