473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

FTP file creation date

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
Jul 18 '05 #1
5 12364
[ 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
Jul 18 '05 #2
[ 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
Jul 18 '05 #3
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", "my@email.com")

# 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
Jul 18 '05 #4
eu****@boardkulture.com ([ EuGeNe ]) wrote in message news:<de**************************@posting.google. com>...
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
Jul 18 '05 #5
Ron, and all,

Thanks for your help! It is working :P

Cheers,

EuGeNe
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Traxinger | last post by:
Is there a function that determines when a file was created? Like if I wanted to return all the files created in the last month from a directory, is there a way to do that? I've googled and googled...
2
by: David Fickbohm | last post by:
People, I am trying to determine the creation date of files in a folder. I am using the following code to find the folder and confirm that files exist in the folder. If someone could give me an...
0
by: nek | last post by:
Greetings, I am running DB2 WSE V8.1, FP5 on W2K. The problem is not directly related to DB2 but as part of job, I need to manipulate files coming in from all sources. Under a main folder,...
4
by: wil | last post by:
Dear All, In the linux platform, is there anyway I can in a C program change the file create date? Thanks, wil.
3
by: Steven Blair | last post by:
Hi, I have a trace log file for a system I am writing. If the creation date is older than 14 days, I have to rename that file (File.Move). The next time a trace message is required a new file is...
6
by: KoRnDragon | last post by:
I know about getlastmod() but is there one for created date? If not is there some other way of getting the created date of a file?
1
by: avinashpr | last post by:
Hi, How do i fetch the information pertaining to a file like file creation date, modified date.....in c++. small code snippet is hieghly appreceated cheers, avinash
3
by: MrDeej | last post by:
Hello! I am designing an import module in my access database for text files it looks like thisDim GammeltNavn As String Dim NyttNavn As String Dim MaskinNavn As String Dim fsoFileSearch As...
2
by: gavin9822623 | last post by:
I need to set a file creation date to "2005/01/01 13:00:00 PM". The date format is (YYYY/MM/DD). Any help or example will be greatly appreciated. Thank you Gavin
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.