ftputil.py

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon

    #1

    ftputil.py

    Hi Everyone,

    I recently installed a module called ftputil and I am trying to
    understand how it works.
    I created a simple program to download about 1500 files from my ftp and
    the program looks like this:

    *************** *************** *************** *************** **************
    #! /usr/bin/env python
    #-*- coding: UTF-8 -*-

    def ftp_fetch(newdi r):
    import sys
    import ftputil
    # download some files from the login directory
    host = ftputil.FTPHost ('ftp.xxxx.xx', 'xxxxxx', 'xxxxxxx')
    names = host.listdir(ho st.curdir)
    print "There are %d files on the FTP server to be downloaded" %
    len(names)
    for name in names:
    if host.path.isfil e(name):
    host.download(n ame, name, 'b') # remote, local, binary
    mode
    #make a new directory and copy a remote file into it
    host.mkdir('new dir')
    source = host.file('inde x.html', 'r') # file-like object
    target = host.file('newd ir/index.html', 'w') # file-like object
    host.copyfileob j(source, target) # similar to shutil.copyfile obj
    source.close()
    target.close()

    return 0

    #--------------------------------
    if __name__== "__main__":
    import os
    import sys
    newdir = os.getcwd()
    print "Downloadin g files from FTP into directory: %s" % newdir
    #sys.exit(0)
    stat = ftp_fetch(newdi r)
    *************** *************** *************** *************** *************** *******

    Now I also need to write another function to upload files and I
    haven't the faintest idea yet.
    Here is the part of the program that I need explained:

    host.download(n ame, name, 'b') # remote, local, binary mode
    source = host.file('inde x.html', 'r') # file-like object
    target = host.file('newd ir/index.html', 'w') # file-like object
    host.copyfileob j(source, target) # similar to shutil.copyfile obj

    Any help would be greatly appreciated!

    Sincerely,
    Sheldon

  • Miki

    #2
    Re: ftputil.py

    Hello Sheldon,
    [color=blue]
    > Here is the part of the program that I need explained:
    >
    > host.download(n ame, name, 'b') # remote, local, binary mode[/color]
    Download file called "name" from host to a local file in the same name,
    use binary mode.
    [color=blue]
    > source = host.file('inde x.html', 'r') # file-like object[/color]
    Open a file (like built-in "open") on remote site in read mode
    [color=blue]
    > target = host.file('newd ir/index.html', 'w') # file-like object[/color]
    Open a file (like built-in "open") on remote site in write mode
    [color=blue]
    > host.copyfileob j(source, target) # similar to shutil.copyfile obj[/color]
    Copy 1'st file to the second.

    HTH,
    Miki
    If it won't be simple, it simply won't be. [Hire me, source code]


    Comment

    • Sheldon

      #3
      Re: ftputil.py

      Thanks!

      Sheldon

      Comment

      Working...