Reading list Switch to dark mode

    Using Python ftplib library for File transfer

    Updated 1 October 2019

    As python have the rich set of library for doing task automation when it comes to file transfer over FTP server there are many libraries available in python like ftplib, ftputil, paramiko, fabric etc.

    In this article, i am going to explain that how you can use ftplib python library for file transfer over an FTP server.

     

    Using ftplib library you can perform a variety of FTP jobs automation, You can seamlessly connect to an FTP server to retrieving files and manipulate them locally.

     

    Searching for an experienced
    Odoo Company ?
    Find out More
    from ftplib import FTP
    ftp_obj = FTP()
    
    serveruri='192.168.1.199'
    serverport='21'
    ftp.connect(serveruri, serverport)
    
    serveruser='ftp-test'
    serverpassword='ftp-test12#'
    ftp_obj.login(user = serveruser, passwd = serverpassword)
    
    ftp_obj.retrlines('LIST') # list directory contents
    
    ##########################################

    FTP Methods:

    ==>FTP.connect(host[, port[, timeout]])

    This use to establish a connection to the given host and port.

    In case no port provided then the default port number is 21 will use as per FTP protocol specification.

     

    ==>FTP.getwelcome()

    This use to get the welcome message sent by the server.

     

    ==>FTP.cwd(pathname)

    To change directory using cwd.

    Like:

    serverdirectorypath='/files'
    ftp_obj.cwd(serverdirectorypath)

     

    ==>FTP.retrbinary(command, callback[, maxblocksize[, rest]])

    This use to retrieve a file in form of binary transfer mode.
    Like:

    ftp.retrbinary('RETR %s'%serverfilepath, open(localfilepath, 'wb').write)

     

    ==>FTP.storbinary(command, fp[, blocksize, callback, rest])

    This use to upload the data over the server.

    ftp_obj.storbinary('STOR %s'%serverfilepath, = open(localfilepath, 'rb'))

     

    ==>FTP.retrlines(command[, callback])

    Like:

    ftp_obj.retrlines('LIST')

    To retrieve a file or directory data listing.

     

    ==>FTP.delete(filename)

    ftp_obj.delete(serverfilepath)

    To delete the file (filename) from the server.

     

    ==>FTP.mkd(pathname)

    To create a new directory on given path (pathname).

    ftp_obj.mkd(serverdirectorypath)

     

    For more detailed elaboration, please see the official python documentation

    That’s all for today. I hope this blog will help you. I’d be very grateful if you’d write your opinions, comments, and suggestions to keep the page updated and interesting.

    You may also like our post on using StringIO and BytesIO for managing data file object .

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • aunsine
    • Archana Tiwari (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home