Ftp delete wildcard?

im trying to delete all text files from an ftp directory

something like this:

from ftplib import FTP
ftp = FTP(‘127.0.0.1’)
ftp.login(’’)
directory = ‘somedirectory’
ftp.cwd(directory)
ftp.delete(’*.txt’)
ftp.close()

is there a way to delete multiple files of the same extension?

Thank you,
Chris

I don’t think so. I think you have to loop through all of the files and delete only the txt documents:files = ftp.nlst() for file in files: if file.find(".txt") != -1: ftp.delete(file)

looks like its trying to delete the text files but the script generates this error

Traceback (most recent call last):

File “”, line 13, in

File “C:\Documents and Settings\cannin\Application Data\Sun\Java\Deployment\cache\FPMI\script_lib2\Lib\ftplib.py”, line 486, in delete

resp = self.sendcmd('DELE ' + filename)

File “C:\Documents and Settings\cannin\Application Data\Sun\Java\Deployment\cache\FPMI\script_lib2\Lib\ftplib.py”, line 241, in sendcmd

return self.getresp()

File “C:\Documents and Settings\cannin\Application Data\Sun\Java\Deployment\cache\FPMI\script_lib2\Lib\ftplib.py”, line 216, in getresp

raise error_perm, resp

ftplib.error_perm: 550 File not found.

and the ftp log screen shot (attached) shows it trying to delete one of my text files “New text document (2).txt” but errors file not found.

Maybe it needs the full path to the file and not just the name?

im not sure but it looks like the ftp.nlst is returning the date and time info as well as the file name and then cant find that name to delete. maybe I need to find a way to strip off the extra file info? been trying to find a way to do that but not much luck so far.

yep I think thats what it was - the 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.

here is what works:

[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]

If anyone can think of a better way to do this I would greatly appreciate it.

Thanks,

Chris

Find the position of the colon and move along 4 characters?

yea, that would work. still seems like there should be a ftp function or method to return just the file name. other python file function like glob work just fine - I havnt been able to find a straight forward way to accomplish this in ftp.

It looks like Python 3.3 has the ability to get a list of file names, but our version of Jython/Python is older. You’ll have to find something similar to what AlThePal suggested, some reference that is guaranteed to grab the filename no matter how the filename is formatted.