ftputil.py

S

Sheldon

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(newdir):
import sys
import ftputil
# download some files from the login directory
host = ftputil.FTPHost('ftp.xxxx.xx', 'xxxxxx', 'xxxxxxx')
names = host.listdir(host.curdir)
print "There are %d files on the FTP server to be downloaded" %
len(names)
for name in names:
if host.path.isfile(name):
host.download(name, name, 'b') # remote, local, binary
mode
#make a new directory and copy a remote file into it
host.mkdir('newdir')
source = host.file('index.html', 'r') # file-like object
target = host.file('newdir/index.html', 'w') # file-like object
host.copyfileobj(source, target) # similar to shutil.copyfileobj
source.close()
target.close()

return 0

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

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(name, name, 'b') # remote, local, binary mode
source = host.file('index.html', 'r') # file-like object
target = host.file('newdir/index.html', 'w') # file-like object
host.copyfileobj(source, target) # similar to shutil.copyfileobj

Any help would be greatly appreciated!

Sincerely,
Sheldon
 
M

Miki

Hello Sheldon,
Here is the part of the program that I need explained:

host.download(name, name, 'b') # remote, local, binary mode
Download file called "name" from host to a local file in the same name,
use binary mode.
source = host.file('index.html', 'r') # file-like object
Open a file (like built-in "open") on remote site in read mode
target = host.file('newdir/index.html', 'w') # file-like object
Open a file (like built-in "open") on remote site in write mode
host.copyfileobj(source, target) # similar to shutil.copyfileobj
Copy 1'st file to the second.

HTH,
Miki
http://pythonwise.blogspot.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top