# Ftp delete wildcard?

**URL:** <https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793>\
**Category:** Ignition\
**Created:** [February 3, 2013, 1:43am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793 "2013-02-03T01:43:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [February 3, 2013, 1:43am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/1 "2013-02-03T01:43:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [February 3, 2013, 8:13pm UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/2 "2013-02-03T20:13:04Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [February 4, 2013, 7:43pm UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/3 "2013-02-04T19:43:35Z")

</div>

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.  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/14232de3510055dc57f0869416cf87bda2de0b6a.jpg)

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [February 5, 2013, 12:43am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/4 "2013-02-05T00:43:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [February 5, 2013, 1:03am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/5 "2013-02-05T01:03:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [February 5, 2013, 2:03am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/6 "2013-02-05T02:03:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [February 5, 2013, 8:42am UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/7 "2013-02-05T08:42:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cja7928](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@cja7928](https://forum.inductiveautomation.com/u/cja7928)\
**Post date:** [February 5, 2013, 4:36pm UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/8 "2013-02-05T16:36:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [February 5, 2013, 5:08pm UTC](https://forum.inductiveautomation.com/t/ftp-delete-wildcard/5793/9 "2013-02-05T17:08:19Z")

</div>

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.
