FTP file creation date

Discussion in 'Python' started by [ EuGeNe ], Oct 27, 2004.

  1. [ EuGeNe ]

    [ EuGeNe ] Guest

    Hi all,

    I would like to write a script that downloads one file from a ftp
    server if the file creation date satisfy a condition.

    I can't figure out how to find from a ftp server what is the creation
    date of the file (using python).

    Any idea?

    Thanks for your help!

    EuGeNe
     
    [ EuGeNe ], Oct 27, 2004
    #1
    1. Advertising

  2. [ EuGeNe ]

    Larry Bates Guest

    [ EuGeNe ] wrote:
    > Hi all,
    >
    > I would like to write a script that downloads one file from a ftp
    > server if the file creation date satisfy a condition.
    >
    > I can't figure out how to find from a ftp server what is the creation
    > date of the file (using python).
    >
    > Any idea?
    >
    > Thanks for your help!
    >
    > EuGeNe


    I don't believe there is anything like .stat methods included
    in ftplib. I have always retrieved a directory of the files and
    extracted the creation dates from each file manually. Then
    acted on them accordingly. You should note that the dates on
    most FTP servers are relative to the clock on that server (not
    your local clock). If the clock on FTP server is different
    from yours, either because it is off or because it is in a
    different timezone, you must adjust accordingly.

    Larry Bates
     
    Larry Bates, Oct 27, 2004
    #2
    1. Advertising

  3. [ EuGeNe ]

    Steve Holden Guest

    [ EuGeNe ] wrote:
    > Hi all,
    >
    > I would like to write a script that downloads one file from a ftp
    > server if the file creation date satisfy a condition.
    >
    > I can't figure out how to find from a ftp server what is the creation
    > date of the file (using python).
    >
    > Any idea?
    >
    > Thanks for your help!
    >
    > EuGeNe


    Take a look at the ftpmirror.py script that comes in the Tools
    directory. It might give you some ideas.

    regards
    Steve
    --
    http://www.holdenweb.com
    http://pydish.holdenweb.com
    Holden Web LLC +1 800 494 3119
     
    Steve Holden, Oct 28, 2004
    #3
  4. [ EuGeNe ]

    Ron Beitel Guest

    Eugene,

    To test for the latest version of a file on the ftpserver,
    use the ftplib.sendcmd() method with the '4-character'
    ftp command for modtime.

    #!/usr/bin/python
    import ftplib

    ftp = ftplib.FTP("ftp.mysite.org")
    f.login("anonymous", "")

    # MDTM is an abbreviated '4-char' ftp command
    modtime = f.sendcmd("MDTM file.name")

    # Strip off the 'reply code'
    if modtime[:3] = "213":
    modtime = modtime[3:].strip()


    Refer to RFC 959 - File Transfer Protocal
    http://www.faqs.org/rfcs/rfc959.html

    Section4.1 lists the abbreviated ftp commands,
    however, 'SIZE' and 'MDTM' are not covered in the RFC
    Section4.2.1 covers the Reply Codes.

    For examples on using f.sendcmd(), check the module's
    source code /usr/lib/python2.2/ftplib.py
    Especially, look at the definitions for
    def size()
    def nlst()


    If you're not concerned with portability, you could modify
    your systems copy of the library. Add....

    def modtime(self, filename):
    '''Retrieve the modtime of a file.'''
    resp = self.sendcmd('MDTM ' + filename)
    if resp[:3] == '213'
    s = resp[3:].strip()
    return s

    Then in your program, you can just say
    t = f.modtime("file.name")

    Ron Beitel
     
    Ron Beitel, Oct 28, 2004
    #4
  5. ([ EuGeNe ]) wrote in message news:<>...
    > Hi all,
    >
    > I would like to write a script that downloads one file from a ftp
    > server if the file creation date satisfy a condition.
    >
    > I can't figure out how to find from a ftp server what is the creation
    > date of the file (using python).
    >
    > Any idea?
    >
    > Thanks for your help!
    >
    > EuGeNe


    import os, ftplib, time

    #==============================================================================
    thisyear = time.localtime().tm_year
    monthdict={'Jan': 1,'Feb': 2,'Mar': 3,'Apr': 4,'May': 5,'Jun':
    6,'Jul': 7,'Aug': 8,'Sep': 9,'Oct': 10,'Nov': 11,'Dec':12}
    #==============================================================================
    def listdir(ftp, path = '.'):
    filetimes={}
    #-------------------
    def callback(line):
    ls = line.split()
    #print ls -> ['drwxrwxr-x', '2', 'ftpclient', 'k3', '335872',
    'Jul', '23', '15:31', 'trash_check']
    if len(ls) == 9:
    access, x, y, z, size, ls_month, ls_day, ls_union,
    ls_filename = ls
    tm_mon = monthdict[ls_month]
    tm_mday = int(ls_day)
    assert len(ls_union) in (4,5)
    if len(ls_union) == 5:
    assert ls_union[2] == ':'
    tm_year = thisyear
    tm_hour, tm_min =
    int(ls_union[0:2]),int(ls_union[3:5])
    elif len(ls_union) == 4:
    tm_year = int(ls_union)
    tm_hour, tm_min = 0, 0

    ftime = time.mktime((tm_year, tm_mon, tm_mday, tm_hour,
    tm_min, 0,0,0,-1))
    filetimes[ls_filename] = ftime
    #------------------
    ftp.dir(path, callback)
    return filetimes
     
    G?nter Jantzen, Oct 29, 2004
    #5
  6. [ EuGeNe ]

    [ EuGeNe ] Guest

    Ron, and all,

    Thanks for your help! It is working :p

    Cheers,

    EuGeNe
     
    [ EuGeNe ], Oct 30, 2004
    #6
    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. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,301
    Michael Borgwardt
    May 30, 2004
  2. Tim Williams

    Re: FTP file creation date

    Tim Williams, Oct 27, 2004, in forum: Python
    Replies:
    0
    Views:
    497
    Tim Williams
    Oct 27, 2004
  3. ruby talk

    FTP folder creation mkdir problem

    ruby talk, Oct 20, 2004, in forum: Ruby
    Replies:
    3
    Views:
    365
    ruby talk
    Oct 20, 2004
  4. Jeff Miller
    Replies:
    0
    Views:
    527
    Jeff Miller
    Mar 26, 2009
  5. D. Buck
    Replies:
    2
    Views:
    531
    D. Buck
    Jun 29, 2004
Loading...

Share This Page