tags:

views:

66

answers:

2
def startConnection(self):
 from ftplib import FTP
 self.ftp = FTP(self.loginServer)
 print 'Loging In'
 print self.ftp.login(self.username, self.password)
 data = []
 self.ftp.dir(data.append)
 for line in data:
  try:
   self.date_str = ' '.join(line.split()[5:8])
   newDate = time.strptime(self.date_str,'%b %d %H:%M')
   print newDate
   col_list = line.split()
   name = col_list[0]
   tempDir = {}

   if name.startswith('d'):
    tempDir['directory'] = newDate
   else:
    tempDir['file']=newDate

   self.dirInfo[col_list[8]] = tempDir
  except:
   print "**********Exception for line **********\n" + line + "\n**********Exception End**********"

This function is working fine, newDate value is Aug 20 11:12, but year is missing, so Bydefault year value it is taking 1900, which is not correct. To debug it, i logged in ftp server and did dir / ls in both the cases it is showing timestamp like 'Aug 20 11:12'. But if i do ls -lTr, in that case it is showing year,

what i want is some how i can pass above command to ftp and get the result. Is there any python ftplib module's function that can do this.

+1  A: 

Try

self.ftp.dir('-lTr', data.append)
Aaron Digulla
worked perfect, Thanks
A: 

If you are using a command that gives a short-form date, or want to cope with different arguments being handed to ls you'll have to make multiple attempts to parse the date with different format strings, until you don't get a ValueError, post-processing the successful parse according to which format it has matched -- files more than a year old will often report a date format including a year but no time of day (here you could probably stick with a default 00:00:00 time); recent files reported in a format without a year can have the current year defaulted in; formats that contain a year would be left unchanged.

Steve Gilham