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

P

pilgrim773

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()
 
M

MRAB

pilgrim773 said:
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')
It's better to pass the full filename instead of changing the directory
because it means that you don't need to keep track of which directory
you're currently in, for example:

files = ftp.retrlines(directory + '/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))

The os module works with _local_ files, not the remote files on a
server.

You can list the files and get info about them like this:

reply = []
ftp.retrlines("LIST " + directory, reply.append)

'reply' will be a list of lines, one per file, which you can then parse.
It shouldn't be too difficult to write a function to hide the messy
details. :)
#except:
#exit ("Cannot delete files")

Bare excepts are almost always a very bad idea.
 
G

Giampaolo Rodolà

2010/5/19 pilgrim773 said:
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()

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
type=file;size=211;perm=r;modify=20071103093626;unique=801e32; module.py

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
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top