Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'mrab': 0.05; 'append': 0.07; 'filename': 0.07; 'thats': 0.07; 'filename,': 0.09; 'files:': 0.09; 'ftplib': 0.09; 'subject:files': 0.09; 'subject:same': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'anyway': 0.11; '"new': 0.16; "'0',": 0.16; '(file': 0.16; '-1:': 0.16; 'bullet': 0.16; 'doesnt': 0.16; 'splits': 0.16; 'stripped': 0.16; 'subject:type': 0.16; 'wow,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'directory.': 0.17; '>>>': 0.18; 'feb': 0.19; 'trying': 0.21; 'import': 0.21; 'assumes': 0.22; 'help.': 0.22; 'cc:2**0': 0.23; 'split': 0.23; 'this:': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; 'this?': 0.28; 'chris': 0.28; 'separated': 0.29; 'worked': 0.30; 'returned': 0.30; 'code': 0.31; '(and': 0.32; 'file': 0.32; 'consist': 0.33; 'entry': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'text': 0.34; 'exist': 0.35; 'so,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'characters': 0.36; 'should': 0.36; 'thank': 0.36; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'delete': 0.38; 'instead': 0.39; 'your': 0.60; 'from:no real name:2**0': 0.60; 'you.': 0.61; 'maximum': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.63; 'great': 0.64; 'of:': 0.65; "'test'": 0.84; '2013': 0.84; 'clearer': 0.84 X-Received: by 10.49.12.238 with SMTP id b14mr2324535qec.18.1360105152617; Tue, 05 Feb 2013 14:59:12 -0800 (PST) Newsgroups: comp.lang.python Date: Tue, 5 Feb 2013 14:59:12 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=199.181.155.130; posting-account=NWckLgoAAAA_ESRKeclDK3J-ZCP6LE-U References: <1d7b7a3d-097f-477b-93b5-4343a25bc1f1@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 199.181.155.130 MIME-Version: 1.0 Subject: Re: ftp - delete multiple files of same type From: chris.annin@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Message-ID: Lines: 271 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360105160 news.xs4all.nl 6909 [2001:888:2000:d::a6]:44244 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38242 >=20 > > im trying to delete all text files from an ftp directory. is there a wa= y to delete multiple files of the same extension? >=20 > > >=20 > > I came up with the following code below which works but I have to appen= d the string because ftp.nlst returns: >=20 > > >=20 > > "-rwx------ 1 user group 0 Feb 04 15:57 New Text Document.txt" >=20 > > >=20 > > 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" >=20 > > >=20 > > so anyway I stripped off the the beginning keeping the last 21 characte= rs and it worked great - this should work being that I know all my text fil= es names are the same length in characters - but it seems like there should= be a better more bullet proof way to do this? >=20 > > >=20 > > [code]import os >=20 > > import system >=20 > > from ftplib import FTP >=20 > > >=20 > > ftp =3D FTP('127.0.0.1') >=20 > > ftp.login('') >=20 > > >=20 > > directory =3D 'test' >=20 > > ftp.cwd(directory) >=20 > > >=20 > > files =3D ftp.nlst() >=20 > > >=20 > > for file in files: >=20 > > if file.find(".txt") !=3D -1: >=20 > > file =3D (file [-21:]) >=20 > > ftp.delete(file) >=20 > > >=20 > > ftp.close()[/code] >=20 > > >=20 > > any ideas on this? thank you. >=20 > > >=20 > Firstly, instead of: >=20 >=20 >=20 > file.find(".txt") !=3D -1 >=20 >=20 >=20 > use: >=20 >=20 >=20 > file.endswith(".txt") >=20 >=20 >=20 > It's clearer (and it's true only if the ".txt" is at the end!) >=20 >=20 >=20 > Secondly, your code assumes that the filename is exactly 21 characters. >=20 > It looks like the strings returned by ftp.nlst() consist of 9 fields >=20 > separated by whitespace, with the last field being the filename, >=20 > which can also contain spaces. That being so, you can split the strings >=20 > like this: >=20 >=20 >=20 > fields =3D file.split(None, 9) >=20 >=20 >=20 > That'll make a maximum of 9 splits on any whitespace, for example: >=20 >=20 >=20 > >>> "-rwx------ 1 user group 0 Feb 04 15:57 New Text=20 >=20 > Document.txt".split(None, 9) >=20 > ['-rwx------', '1', 'user', 'group', '0', 'Feb', '04', '15:57', 'New',=20 >=20 > 'Text Document.txt'] >=20 >=20 >=20 > Therefore: >=20 >=20 >=20 > for entry in ftp.nlst(): >=20 > if entry.endswith(".txt"): >=20 > filename =3D entry.split(None, 9)[-1] >=20 > ftp.delete(filename) On Tuesday, February 5, 2013 10:45:02 AM UTC-8, MRAB wrote: > On 2013-02-05 17:29, chris.annin@gmail.com wrote: >=20 > > im trying to delete all text files from an ftp directory. is there a wa= y to delete multiple files of the same extension? >=20 > > >=20 > > I came up with the following code below which works but I have to appen= d the string because ftp.nlst returns: >=20 > > >=20 > > "-rwx------ 1 user group 0 Feb 04 15:57 New Text Document.txt" >=20 > > >=20 > > 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" >=20 > > >=20 > > so anyway I stripped off the the beginning keeping the last 21 characte= rs and it worked great - this should work being that I know all my text fil= es names are the same length in characters - but it seems like there should= be a better more bullet proof way to do this? >=20 > > >=20 > > [code]import os >=20 > > import system >=20 > > from ftplib import FTP >=20 > > >=20 > > ftp =3D FTP('127.0.0.1') >=20 > > ftp.login('') >=20 > > >=20 > > directory =3D 'test' >=20 > > ftp.cwd(directory) >=20 > > >=20 > > files =3D ftp.nlst() >=20 > > >=20 > > for file in files: >=20 > > if file.find(".txt") !=3D -1: >=20 > > file =3D (file [-21:]) >=20 > > ftp.delete(file) >=20 > > >=20 > > ftp.close()[/code] >=20 > > >=20 > > any ideas on this? thank you. >=20 > > >=20 > Firstly, instead of: >=20 >=20 >=20 > file.find(".txt") !=3D -1 >=20 >=20 >=20 > use: >=20 >=20 >=20 > file.endswith(".txt") >=20 >=20 >=20 > It's clearer (and it's true only if the ".txt" is at the end!) >=20 >=20 >=20 > Secondly, your code assumes that the filename is exactly 21 characters. >=20 > It looks like the strings returned by ftp.nlst() consist of 9 fields >=20 > separated by whitespace, with the last field being the filename, >=20 > which can also contain spaces. That being so, you can split the strings >=20 > like this: >=20 >=20 >=20 > fields =3D file.split(None, 9) >=20 >=20 >=20 > That'll make a maximum of 9 splits on any whitespace, for example: >=20 >=20 >=20 > >>> "-rwx------ 1 user group 0 Feb 04 15:57 New Text=20 >=20 > Document.txt".split(None, 9) >=20 > ['-rwx------', '1', 'user', 'group', '0', 'Feb', '04', '15:57', 'New',=20 >=20 > 'Text Document.txt'] >=20 >=20 >=20 > Therefore: >=20 >=20 >=20 > for entry in ftp.nlst(): >=20 > if entry.endswith(".txt"): >=20 > filename =3D entry.split(None, 9)[-1] >=20 > ftp.delete(filename) wow, thank you very much thats a huge help. Chris