Having trouble extracting useful directory details from ftplib.FTP

Discussion in 'Python' started by Stephen Horne, Feb 21, 2004.

  1. Just recently I decided I want to make use of my ISP freebie webspace.
    In order to make that easier, I'd like to be able to automatically
    synchronise an FTP file/folder heirarchy with one on my local hard
    drive. I figured this should be easily handled in Python, and broadly
    speaking it is, but I am having a little difficulty.

    You see, in order to handle the synchronise correctly, I need to be
    able to determine what is on the FTP server to start with. I need to
    be able to recursively search the folders on the FTP server. But so
    far as I can tell, I'm having to rely on some very flaky techniques
    and some observations about my ISPs FTP server that could potentially
    change.

    I tried using FTP.nlst(pathname) but this wasn't very helpful...

    1. It created a bogus first entry in the list - a line saying
    "Found 1" (irrespective of the actual number of items found)
    IIRC.

    2. It provided no indication of whether each item was a file or
    a folder.

    Therefore, I switched to using the following piece of code...

    filelist=[]
    ftp.retrlines ("LIST "+pathname, filelist.append)

    I still get a bogus line at the top, but that's easily worked around.
    The important thing is that by testing out the results, I found that
    for the ftp server I am using I can look at the first character for
    the directory flag, and use the slice [53:] to extract the filename
    from each line.

    The problem is, however, that this seems like skating on very thin
    ice. So far as I can tell, there is no standard for the format
    reported by the LIST command.

    This so far is enough - I can use this to find all files and folders
    on the FTP server, and I can download backup copies to a hard drive
    folder and then delete them all before uploading the replacement
    files, all in one automatic process that (at the moment) just works.
    But - apart from the fact that I'm very nervous about it - ideally,
    I'd like to do a more sane synchronise - only delete files that need
    to be deleted, and only upload files that have changed. To do that, I
    need to get more details about the files on the FTP server - time and
    date stamps in particular. Getting hold of this information looks like
    being at least as much of a hack as what I've done already.

    So the question is... Is there some better way of handling this that
    I'm missing, or is this just the way it is? After all, there are a lot
    of FTP client utilities out there that seem to get this information
    perfectly reliably, which seems surprising if they have to rely on
    parsing an inconsistent directory listing format.

    Any hints?


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
     
    Stephen Horne, Feb 21, 2004
    #1
    1. Advertising

  2. Stephen Horne

    Paul McGuire Guest

    "Stephen Horne" <> wrote in message
    news:...
    >
    > Just recently I decided I want to make use of my ISP freebie webspace.
    > In order to make that easier, I'd like to be able to automatically
    > synchronise an FTP file/folder heirarchy with one on my local hard
    > drive. I figured this should be easily handled in Python, and broadly
    > speaking it is, but I am having a little difficulty.
    >

    <snip>
    Is this anything like ftpmirror.py? You'll find it in the tools\scripts
    directory of the Python distribution.

    -- Paul
     
    Paul McGuire, Feb 22, 2004
    #2
    1. Advertising

  3. On Sun, 22 Feb 2004 02:42:59 GMT, "Paul McGuire"
    <_rr.com> wrote:

    >"Stephen Horne" <> wrote in message
    >news:...
    >>
    >> Just recently I decided I want to make use of my ISP freebie webspace.
    >> In order to make that easier, I'd like to be able to automatically
    >> synchronise an FTP file/folder heirarchy with one on my local hard
    >> drive. I figured this should be easily handled in Python, and broadly
    >> speaking it is, but I am having a little difficulty.
    >>

    ><snip>
    >Is this anything like ftpmirror.py? You'll find it in the tools\scripts
    >directory of the Python distribution.


    Thanks.

    It's the exact reverse of what I want (it mirrors from the server to
    the local disk, I want to mirror from the disk to the server) but it
    is extremely clueful - the way it assumes a standard (unix oriented)
    format for the listing unless a Mac server option is specified tells
    me precisely what I need to know.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
     
    Stephen Horne, Feb 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikael
    Replies:
    0
    Views:
    412
    Mikael
    Jul 14, 2004
  2. Nico Grubert
    Replies:
    0
    Views:
    645
    Nico Grubert
    Nov 24, 2005
  3. Nico Grubert
    Replies:
    0
    Views:
    449
    Nico Grubert
    Nov 25, 2005
  4. Sky Larking
    Replies:
    4
    Views:
    622
    Sky Larking
    Feb 25, 2010
  5. D. Buck
    Replies:
    2
    Views:
    493
    D. Buck
    Jun 29, 2004
Loading...

Share This Page