newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question

Chris Angelico rosuav at gmail.com
Sun Sep 16 11:41:52 EDT 2012


On Mon, Sep 17, 2012 at 1:09 AM, moonhkt <moonhkt at gmail.com> wrote:
> Hi All
>
> O'Reilly Book ISBN 978-986-6840-36-4.
>
> python --version
> Python 2.6.2 on AIX 5.3

Hi! Thanks for this, good information to open with.

> 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 <module>
>     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

NoneType is the type of the singleton object None. Why are you getting
None where you ought to be providing a directory name? Heavily trimmed
code follows:

>     def __init__(self, host, username, password, ftp_base_dir,
>                                 local_base_dir, delete=False):
>         self.conn.cwd(ftp_base_dir)
>     parser.add_option("-r", "--remote_dir", dest="remote_dir",
>          action='store', help="FTP remote starting directory")
>     f = FTPSync(options.host, options.username, options.password,
>             options.remote_dir, options.local_dir, options.delete)

If you don't pass -r/--remote_dir to your script, then (presumably - I
haven't actually used optparse so this is glarked from context)
options.remote_dir is being set to, or left at, None. A quick look at
the docs suggests that one solution is to add a default value to the
options:

http://docs.python.org/library/optparse.html#default-values

Alternatively, treat the options as mandatory, and provide them on the
command line.

Side point: When you go to the docs page there, you may notice that
optparse is deprecated in favour of argparse. May as well continue the
tutorial with what they recommend, but it's probably worth having a
look at argparse eventually.

Hope that helps!

Chris Angelico



More information about the Python-list mailing list