So a little play with Python gave me this...
…it should save a fair few cumulative hours, but more importantly, it'll save a fair few hours of tedium.
from ftplib import FTP
def clearFTPDirectory(address,
username,
directory):
server=FTP(address)
server.login(username)
server.cwd(directory)
directory=[]
server.dir(directory.append)
# list comprehension, baby!
filenames=[entry.split(':')[-1][3:] for entry in directory]
for filename in filenames:
try:
server.delete(filename)
print 'Deleted:%s'%filename
except:
print ('failed to delete "%s"'%filename)
server.quit()
The bit that tripped me up for a little while was server.dir(directory.append) which takes the append function as a parameter. Makes perfect sense now.
Note: if you use this, you may need to check the format of the strings returned by dir(), and adjust the line that extracts the filename.
No comments:
Post a Comment