Delete files from FTP Server older then 7 days. Using ftputil and ftplib.

pilgrim773 thomas.fischer at gmail.com
Wed May 19 10:41:38 EDT 2010


Hello I am new to Python programming. I need a write a script which
will delete files from a FTP server after they have reached a certain
age, like 7 days for example. I have prepared this code below, but I
get an error message:
The system cannot find the path specified: '/test123/*.*' Probably
someone can help me further? Thank you in advance!

import os, time, ftputil
from ftplib import FTP

ftp = FTP('127.0.0.1')
print "Automated FTP Maintainance"
print 'Logging in.'
ftp.login('admin', 'admin')

# This is the directory that we want to go to
directory = 'test123'
print 'Changing to:' + directory
ftp.cwd(directory)
files = ftp.retrlines('LIST')
print 'List of Files:' + files
# ftp.remove('LIST')

#-------------------------------------------
now = time.time()
for f in os.listdir(directory):
    if os.stat(f).st_mtime < now - 7 * 86400:
        if os.directory.isfile(f):
           os.remove(os.directory.join(directory, f))
#except:
    #exit ("Cannot delete files")
#-------------------------------------------

print 'Closing FTP connection'
ftp.close()




More information about the Python-list mailing list