FAQ
I have read ftpmirror.py but I can' manage to do what I want : I'd like
a script that read ftp directories recursively (ident if a file or a
directory) and puts all pdf's documents in a list.

Has anyone could give me an example of a function that read contents and
make the difference between files and directory.

Thanks for reading.

Conatic

Search Discussions

  • Simon place at Aug 6, 2003 at 11:29 pm
    On the ftp holding my web space (ftp server: ProFTPD 1.2.0pre10 ) a 'stat
    *.pdf' command will list all pdf files in all sub directories recursively, (
    and this is meant to be fairly standard,) unfortunately using ftp commands
    directly shows up the server os dependent format that directories are listed
    in ( usually UNIX ) but the functions to interpret this are in the ftp lib and
    there's a more general interpretation module out there somewhere ( handles
    more server os's )
  • Conatic at Aug 7, 2003 at 8:37 pm

    On Thu, 07 Aug 2003 00:29:21 +0100, simon place wrote:
    On the ftp holding my web space (ftp server: ProFTPD 1.2.0pre10 ) a 'stat
    *.pdf' command will list all pdf files in all sub directories recursively, (
    and this is meant to be fairly standard,) unfortunately using ftp commands
    directly shows up the server os dependent format that directories are listed
    in ( usually UNIX ) but the functions to interpret this are in the ftp lib and
    there's a more general interpretation module out there somewhere ( handles
    more server os's )
    With 'stat *.pdf' and classic ftplib I have this. Have you got same ?
    WHere is the mistake ?

    from ftplib import FTP
    ftp = FTP('ftp.python.org') # connect to host, default port
    ftp.login()
    ftp.retrlines('stat *.pdf') # list directory contents
    ftp.quit()

    -----------------------------------------------------------------------
    python -u ftp.py
    Traceback (most recent call last):
    File "ftp.py", line 4, in ?
    ftp.retrlines('stat *.pdf') # list directory contents
    File "/usr/lib/python2.2/ftplib.py", line 402, in retrlines
    conn = self.transfercmd(cmd)
    File "/usr/lib/python2.2/ftplib.py", line 346, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
    File "/usr/lib/python2.2/ftplib.py", line 330, in ntransfercmd
    raise error_reply, resp
    ftplib.error_reply: 211-status of *.pdf:
    211 End of Status
    Exit code: 1
  • Simon place at Aug 7, 2003 at 10:39 pm
    according to the info i've got reply '211' like all the '2??' replies are
    confirmation of a correctly completed command?

    'stat' transfers its result over the control socket, i think retrlines is
    looking for a result on the data socket, which is what the 'list' command
    does, or 'nlst' which just includes file names.

    so:

    from ftplib import FTP
    ftp = FTP('ftp.python.org') # connect to host, default port
    ftp.login()
    a=ftp.sendcmd('stat *',a.append)
    ftp.quit()
    print a

    or for a list;

    from ftplib import FTP
    ftp = FTP('ftp.python.org') # connect to host, default port
    ftp.login()
    a=[]
    ftp.retrlines('list *',a.append)
    ftp.quit()
    print a

    or for a file name list;

    from ftplib import FTP
    ftp = FTP('ftp.python.org') # connect to host, default port
    ftp.login()
    a=[]
    ftp.retrlines('nlst *',a.append)
    ftp.quit()
    print a

    unfortunately none of these do a recursive search if you add the extension
    ,so 'nlst *.pdf' only searches the current directory, but your going to have
    to manipulate the list returned anyway so filtering pdf is trivial.
  • Simon place at Aug 7, 2003 at 5:31 pm
    another idea

    the distribution included module 'ftputil' has a walk function to move through
    a ftp directory structure and apply a function to the directory/file name.
  • Conatic at Aug 11, 2003 at 3:57 pm
    Is there a way to check if it is a directory or a file ?

    I 'd like to build a function to make a walk-into in the
    directories but I need a way to know if it is a directory or a file.

    Could you give me any hints ?

    Conatic


    On Thu, 07 Aug 2003 18:31:47 +0100, simon place wrote:

    another idea

    the distribution included module 'ftputil' has a walk function to move through
    a ftp directory structure and apply a function to the directory/file name.
  • Simon place at Aug 12, 2003 at 5:09 pm
    isn't that just a rewrite of the built-in walk function?

    but if you want check ftputil again, it has a isdir() function that calls
    'LIST' and interprets the result, this i think is what makes the walk function
    very slow, since it has to call 'LIST' and make a new socket for the reply, a
    very large number of times, a walk on my site ( 50 files 5 dirs. ) took
    several minutes,

    an idea.
    a rewrite of walk that does 'CWD' (change working directory) and using the
    reply code to tell retrospectively if its a directory, might not be a lot more
    efficient, the current method has the benefit that it gets file size, dates
    etc., but due the way its coded does a 'LIST' for each file!, for a walk you
    only need to know if its a directory and you'll have to do a 'CWD' anyway,
    also you don't generate any new sockets.
  • Conatic at Aug 10, 2003 at 9:23 pm
    Is there a way to check if it is a directory or a file ?

    I 'd like to build a function to make a walk-into in the
    directories but I need a way to know if it is a direcyory or a file.

    Could you give me any hints ?

    Conatic

    On Wed, 06 Aug 2003 23:45:44 +0200, conatic wrote:


    I have read ftpmirror.py but I can' manage to do what I want : I'd like
    a script that read ftp directories recursively (ident if a file or a
    directory) and puts all pdf's documents in a list.

    Has anyone could give me an example of a function that read contents and
    make the difference between files and directory.

    Thanks for reading.

    Conatic

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 6, '03 at 9:45p
activeAug 12, '03 at 5:09p
posts8
users2
websitepython.org

2 users in discussion

Simon place: 4 posts Conatic: 4 posts

People

Translate

site design / logo © 2022 Grokbase