Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'append': 0.07; 'python': 0.09; 'filename,': 0.09; 'files:': 0.09; 'ftplib': 0.09; 'robust': 0.09; 'subject:files': 0.09; 'subject:same': 0.09; 'anyway': 0.11; '"new': 0.16; '(file': 0.16; '-1:': 0.16; 'bullet': 0.16; 'doesnt': 0.16; 'stripped': 0.16; 'subject:type': 0.16; 'string': 0.17; 'wrote:': 0.17; 'directory.': 0.17; 'library,': 0.17; 'feb': 0.19; 'trying': 0.21; 'import': 0.21; 'seems': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'besides': 0.27; 'forgot': 0.27; 'this?': 0.28; 'worked': 0.30; 'version,': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'running': 0.32; 'choices': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'exist': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'characters': 0.36; 'url:library': 0.36; 'should': 0.36; 'thank': 0.36; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'url:docs': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'you.': 0.61; 'email addr:gmail.com': 0.63; 'more': 0.63; 'great': 0.64; 'received:74.208': 0.71; "'test'": 0.84; "it'd": 0.84; 'played': 0.84 Date: Tue, 05 Feb 2013 13:24:16 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: ftp - delete multiple files of same type References: <1d7b7a3d-097f-477b-93b5-4343a25bc1f1@googlegroups.com> In-Reply-To: <1d7b7a3d-097f-477b-93b5-4343a25bc1f1@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:i7GlzfB2YW1mLt8cX2zMtVqUiOuotITQ/t5mwNpBMTb 8ITFb7L+WhHzsNMpw5uWoWcWOaNtQVcAsX8hxSrQIcHfC3RAsR IEMCrHOy7C4NVVh/xTEu6Solcyrx3hQZRi9+PqRQeqshkJM0bV FOKeuZ5OtSlHBFKqU3yFtgrpHQO6fdOM7unnRTQpaGB6tS29bI TxHCuLeZz4VwXHH5qIBbBbqbNN6IqlMF/1sCjDjG7rnY2TRJHm WPBU4Ob+fwr5/hSQnYtXUwAi0BNxt0VQjIOWCbL+oNz4mM2oOV 8huNxKdvJ7LebU7tFBZ2gM2gBlzNZO8MLd1GZK3QdcXXWvQzw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360088678 news.xs4all.nl 6949 [2001:888:2000:d::a6]:60595 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38212 On 02/05/2013 12:29 PM, chris.annin@gmail.com wrote: > 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. > You forgot to say what python version, and what OS you're running on. I havne't played much with the ftp library, but it seems it'd be much more robust to use something like file[39:] but that's still a "magic number". So i look in the ftplib docs: http://docs.python.org/2/library/ftplib.html and it seems there are other choices besides nlst(). If all you need is the filename, why not check out FTP.retrlines(command[, callback])¶ FTP.dir(argument[, ...]) -- DaveA