Take the 2-minute tour ×

quick and simple:

I have the following function, works well if i specify the file name.

import os
import ftplib

def ftpcon(self, host, port, login, pwd, path):

    ftp = ftplib.FTP()
    ftp.connect(host, port, 20)

        ftp.login(login, pwd)

        for files in ftp.nlst():

            if files.endswith('.doc') or files.endswith('.DOC'):

                ftp.retrbinary('RETR ' + files, open(file, 'wb').write)
                print files

But when i use the for loop with ftp.nlst() to try to match an specific type of file, i receive the error:

coercing to Unicode: need string or buffer, type found

Since im not sure if this is the best way to do it, what could the "correct" way to download a file ?

share|improve this question
add comment

1 Answer

Maybe try:

from ftplib import FTP

server = FTP("ip/serveradress")
server.login("user", "password")

server.retrlines("LIST") # Will show a FTP content list.
server.cwd("Name_Of_Folder_in_FTP_to_browse_to") # browse to folder containing your file for DL


server.sendcmd("TYPE i") # ready for file transfer
server.retrbinary("RETR %s"%("FILENAME+EXT to DL"), open("DESTINATIONPATH+EXT", "wb").write) # this will transfer the selected file...to selected path/file

believe this is as correct as serves..

u can set server.set_debuglevel(0) to (1) or (2) for more detailed description while logged in to server.

share|improve this answer
add comment

Your Answer


