ftp - delete multiple files of same type

chris.annin at gmail.com chris.annin at gmail.com
Tue Feb 5 18:29:18 CET 2013


im trying to delete all text files from an ftp directory. is there a way to delete multiple files of the same extension?

I came up with the following code below which works but I have to append the string because ftp.nlst returns:

"-rwx------ 1 user group 0 Feb 04 15:57 New Text Document.txt"

but then when I try to delete it that long file name which includes the date doesnt exist - the files name is "new text document.txt" not "-rwx------ 1 user group 0 Feb 04 15:57 New Text Document.txt"

so anyway I stripped off the the beginning keeping the last 21 characters and it worked great - this should work being that I know all my text files names are the same length in characters - but it seems like there should be a better more bullet proof way to do this?

[code]import os
import system
from ftplib import FTP

ftp = FTP('127.0.0.1')
ftp.login('')

directory = 'test'
ftp.cwd(directory)

files = ftp.nlst()

for file in files:
    if file.find(".txt") != -1:
        file = (file [-21:])
        ftp.delete(file)

ftp.close()[/code]

any ideas on this?  thank you.



More information about the Python-list mailing list