Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'subject:Python': 0.05; 'finally:': 0.05; '__name__': 0.07; 'args)': 0.07; 'host,': 0.07; 'optparse': 0.07; 'parser': 0.07; 'try:': 0.07; 'username,': 0.07; 'subject:question': 0.08; 'python': 0.09; "%s'": 0.09; '__init__': 0.09; 'aix': 0.09; 'cmd': 0.09; 'dirname': 0.09; 'files:': 0.09; 'ftplib': 0.09; 'os.getcwd()': 0.09; 'password)': 0.09; 'sep': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "'__main__':": 0.16; '--version': 0.16; '11:09': 0.16; 'concatenate': 0.16; 'cwd': 0.16; 'isbn': 0.16; "o'reilly": 0.16; 'oserror:': 0.16; 'received:mail-wi0-f178.google.com': 0.16; 'run(self):': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'parameters': 0.20; 'skip:" 40': 0.20; 'import': 0.21; 'error.': 0.21; 'are.': 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'host': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'am,': 0.27; 'skip:# 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'url:mailman': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'source': 0.29; 'class': 0.29; 'error': 0.30; 'server.': 0.32; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'traceback': 0.33; 'received:google.com': 0.34; 'remote': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'except': 0.36; 'url:org': 0.36; 'subject:" ': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'comment': 0.38; 'skip:o 20': 0.38; 'delete': 0.38; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'started.': 0.65; 'subject:skip:A 10': 0.75; 'joel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=te9Z0+NRzstZIi0v94Clqci5Eos3ROtBYdUukrlekuU=; b=095RWbwHFUf+ZDOjKPJ+iAVr3QPImcnrUzhh/Ugj5xFT2sOaRaKSt0emG95gGkncK6 HqB/Z1ym5/U+ZDBeKgZZircN3Wpenu5P/Ej/HOb7D1JEswqggXyAVCNUDJ5kXIJzk9KD uEAo+55IhWv4tWSicttPRM/tUlKDFGZw4R63KgLSbNS641b/KkaiWON8fwQ0FZ/J9ZKA ZWhFsFqzYo/uDWwGLL3+dvlaBMEXesDfJ+V/zbHNJaS2EE20TDX76AuuBEM/H1xtrvG0 noM+RMD2I7HEaqS7ZBEoYoNW3mtYVYWfeOIvsGDI/ZkfHz6OiQb4HFaEURWrHrz1OErE 1FNw== MIME-Version: 1.0 In-Reply-To: <013b5f8b-d599-429b-9c07-3ff7306f4d5f@wz4g2000pbc.googlegroups.com> References: <013b5f8b-d599-429b-9c07-3ff7306f4d5f@wz4g2000pbc.googlegroups.com> Date: Sun, 16 Sep 2012 11:33:19 -0400 Subject: Re: newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question From: Joel Goldstick To: moonhkt Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 125 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347809600 news.xs4all.nl 6915 [2001:888:2000:d::a6]:55828 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29316 On Sun, Sep 16, 2012 at 11:09 AM, moonhkt wrote: > Hi All > > > O'Reilly Book ISBN 978-986-6840-36-4. > > python --version > Python 2.6.2 on AIX 5.3 > > Using this python to get files in ftp server. > > I got below error. What is the error meaning and how to fix ? > > ftp_mirror.py > Traceback (most recent call last): > File "/xx../shell/ftpmirror.py", line 80, in > f = FTPSync(options.host, options.username, options.password, > options.remote_dir, options.local_dir, opti > ons.delete) > File "xx../shell/ftpmirror.py", line 17, in __init__ > self.conn.cwd(ftp_base_dir) > File "/opt/freeware/lib/python2.6/ftplib.py", line 536, in cwd > cmd = 'CWD ' + dirname > TypeError: cannot concatenate 'str' and 'NoneType' objects > > Source : > > #!/usr/bin/env python > > import ftplib > import os > > class FTPSync(object): > def __init__(self, host, username, password, ftp_base_dir, > local_base_dir, delete=False): > self.host = host > self.username = username > self.password = password > self.ftp_base_dir = ftp_base_dir > self.local_base_dir = local_base_dir > self.delete = delete > > self.conn = ftplib.FTP(host, username, password) > self.conn.cwd(ftp_base_dir) > try: > os.makedirs(local_base_dir) > except OSError: > pass > os.chdir(local_base_dir) > def get_dirs_files(self): > dir_res = [] > self.conn.dir('.', dir_res.append) > files = [f.split(None, 8)[-1] for f in dir_res if > f.startswith('-')] > dirs = [f.split(None, 8)[-1] for f in dir_res if > f.startswith('d')] > return (files, dirs) > def walk(self, next_dir): > print "Walking to", next_dir > self.conn.cwd(next_dir) > try: > os.mkdir(next_dir) > except OSError: > pass > os.chdir(next_dir) > > ftp_curr_dir = self.conn.pwd() > local_curr_dir = os.getcwd() > > files, dirs = self.get_dirs_files() > print "FILES:", files > print "DIRS:", dirs > for f in files: > print next_dir, ':', f > outf = open(f, 'wb') > try: > self.conn.retrbinary('RETR %s' % f, outf.write) > finally: > outf.close() > if self.delete: > print "Deleting", f > self.conn.delete(f) > for d in dirs: > os.chdir(local_curr_dir) > self.conn.cwd(ftp_curr_dir) > self.walk(d) > > def run(self): > self.walk('.') > > > if __name__ == '__main__': > from optparse import OptionParser > parser = OptionParser() > parser.add_option("-o", "--host", dest="host", > action='store', help="FTP host") > parser.add_option("-u", "--username", dest="username", > action='store', help="FTP username") > parser.add_option("-p", "--password", dest="password", > action='store', help="FTP password") > parser.add_option("-r", "--remote_dir", dest="remote_dir", > action='store', help="FTP remote starting directory") > parser.add_option("-l", "--local_dir", dest="local_dir", > action='store', help="Local starting directory") > parser.add_option("-d", "--delete", dest="delete", default=False, > action='store_true', help="use regex parser") > > (options, args) = parser.parse_args() comment the next line , then print the parameters and see what they are. That should get you started. > #f = FTPSync(options.host, options.username, options.password, > # options.remote_dir, options.local_dir, options.delete) f = print(options.host, options.username, options.password, options.remote_dir, options.local_dir, options.delete) > f.run() > > -- > http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick