How to delete files with a Python script from a FTP server which are older than 7 days?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-05-19T15:57:32Z Indexed on 2010/05/19 16:00 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

Hello I would like to write a Python script which allows me to delete files from a FTP Server after they have reached a certain age. I prepared the scipt below but it throws the error message: WindowsError: [Error 3] The system cannot find the path specified: '/test123/*.*' Do someone have an idea how to resolve this issue? Thank you in advance!

import os, time
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()

© Stack Overflow or respective owner

Related posts about python

Related posts about ftp