kroepoek85

FTPlib datetime

Discussion created by kroepoek85 on Mar 13, 2014
I am experimenting a little bit with python.
This is the code I wrote at the moment

import os
from ftplib import FTP
ftp = FTP('??????')
ftp.login('??????','??????')
ftp.cwd('USBDISK2-0-01')
ftp.cwd('Progen')
count = 0
for root, dirs, files in os.walk('/home/anne/Test2'):
    for fname in files:
        full_fname = os.path.join(root, fname)
        ftp.storbinary('STOR ' + fname, open(full_fname, 'rb'))
        count += 1
        print(fname + ' correct')
ftp.quit()
print('Totaal: ' + str(count) + ' bestanden')


This code only upload the data from directory Test2 to the ftp server. This part is working perfectly.

Now I want to check if the current file is on the ftp server. Therefore I wrote this script:

import os
from ftplib import FTP
from datetime import datetime
import time
ftp = FTP('???????')
ftp.login('??????','???????')
ftp.cwd('USBDISK2-0-01')
ftp.cwd('Progen')

ftp.retrlines('LIST')

filenames = []
ftp.retrlines('NLST', filenames.append)
for filename in filenames:
    datetimeftp = ftp.sendcmd('MDTM ' + filename)
    datetimepc = os.path.getmtime('/home/anne/Test2/' + filename)
    modifiedTimeFtp = datetime.strptime(datetimeftp[4:], "%Y%m%d%H%M%S").strftime("%d %b %Y %H:%M:%S")
    modifiedTimePc = datetime.fromtimestamp(datetimepc).strftime("%d %b %Y %H:%M:%S")
    if modifiedTimeFtp > modifiedTimePc:
        print modifiedTimeFtp
        print modifiedTimePc
        print "FTP file is recent"
        print "-------"
    else:
        print modifiedTimeFtp
        print modifiedTimePc
        print "Local file is recent"
        print "-------"


The output of this code is:

-rwxrwxrwx 1 ftp ftp 67 Mar 13 18:59 test
13 Mar 2014 17:59:45
13 Mar 2014 18:26:47
Local file is recent
-------

What I think is really strange is that the file on the ftpserver is 18:59 but the MDTM command is giving me 17:59. I thought that the ftpserver had a wrong timezone but the timezone on the ftpserver and my computer are the same. Can someone explain to me what I am doing wrong here?

Outcomes