From: Giampaolo RodolĂ  on
2010/5/19 pilgrim773 <thomas.fischer(a)>:
> 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('')
> 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.remove(, f))
> #except:
> #exit ("Cannot delete files")
> #-------------------------------------------
> print 'Closing FTP connection'
> ftp.close()
> --

Instead of parsing the LIST response you can use MDTM command instead
(if the server supports it) which returns a standardized time-like
response for every file (ftp.sendcmd('MDTM filename')).

Alternatively you can do as you're doing now but using MLSD instead of
LIST (again, the server must support it).
MLSD returns something like this:

type=file;size=156;perm=r;modify=20071029155301;unique=801cd2; music.mp3
type=dir;size=0;perm=el;modify=20071127230206;unique=801e33; ebooks

This would also be a lot cheaper in terms of data exchanged between
client and server and hence faster as you proceed "per-directory"
instead of "per-file in every directory".
Note that if the server is RFC-compliant both MDTM and MLSD times are
expressed as GMT times so an extra conversion to your local time may
be necessary.

Hope this helps,

--- Giampaolo