Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29314 > unrolled thread

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

Started bymoonhkt <moonhkt@gmail.com>
First post2012-09-16 08:09 -0700
Last post2012-09-17 01:41 +1000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question moonhkt <moonhkt@gmail.com> - 2012-09-16 08:09 -0700
    Re: newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-16 11:33 -0400
    Re: newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question Chris Angelico <rosuav@gmail.com> - 2012-09-17 01:41 +1000

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

Frommoonhkt <moonhkt@gmail.com>
Date2012-09-16 08:09 -0700
Subjectnewbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question
Message-ID<013b5f8b-d599-429b-9c07-3ff7306f4d5f@wz4g2000pbc.googlegroups.com>
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 <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

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()
    f = FTPSync(options.host, options.username, options.password,
            options.remote_dir, options.local_dir, options.delete)
    f.run()

[toc] | [next] | [standalone]


#29316

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2012-09-16 11:33 -0400
Message-ID<mailman.794.1347809600.27098.python-list@python.org>
In reply to#29314
On Sun, Sep 16, 2012 at 11:09 AM, moonhkt <moonhkt@gmail.com> 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 <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
>
> 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

[toc] | [prev] | [next] | [standalone]


#29320

FromChris Angelico <rosuav@gmail.com>
Date2012-09-17 01:41 +1000
Message-ID<mailman.796.1347810114.27098.python-list@python.org>
In reply to#29314
On Mon, Sep 17, 2012 at 1:09 AM, moonhkt <moonhkt@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web