Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38242
| 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 | <chris.annin@gmail.com> |
| 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 | <mailman.1378.1360089902.2939.python-list@python.org> |
| 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> <mailman.1378.1360089902.2939.python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Message-ID | <mailman.1395.1360105160.2939.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
>
> > 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.
>
> >
>
> Firstly, instead of:
>
>
>
> file.find(".txt") != -1
>
>
>
> use:
>
>
>
> file.endswith(".txt")
>
>
>
> It's clearer (and it's true only if the ".txt" is at the end!)
>
>
>
> Secondly, your code assumes that the filename is exactly 21 characters.
>
> It looks like the strings returned by ftp.nlst() consist of 9 fields
>
> separated by whitespace, with the last field being the filename,
>
> which can also contain spaces. That being so, you can split the strings
>
> like this:
>
>
>
> fields = file.split(None, 9)
>
>
>
> That'll make a maximum of 9 splits on any whitespace, for example:
>
>
>
> >>> "-rwx------ 1 user group 0 Feb 04 15:57 New Text
>
> Document.txt".split(None, 9)
>
> ['-rwx------', '1', 'user', 'group', '0', 'Feb', '04', '15:57', 'New',
>
> 'Text Document.txt']
>
>
>
> Therefore:
>
>
>
> for entry in ftp.nlst():
>
> if entry.endswith(".txt"):
>
> filename = entry.split(None, 9)[-1]
>
> 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:
>
> > 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.
>
> >
>
> Firstly, instead of:
>
>
>
> file.find(".txt") != -1
>
>
>
> use:
>
>
>
> file.endswith(".txt")
>
>
>
> It's clearer (and it's true only if the ".txt" is at the end!)
>
>
>
> Secondly, your code assumes that the filename is exactly 21 characters.
>
> It looks like the strings returned by ftp.nlst() consist of 9 fields
>
> separated by whitespace, with the last field being the filename,
>
> which can also contain spaces. That being so, you can split the strings
>
> like this:
>
>
>
> fields = file.split(None, 9)
>
>
>
> That'll make a maximum of 9 splits on any whitespace, for example:
>
>
>
> >>> "-rwx------ 1 user group 0 Feb 04 15:57 New Text
>
> Document.txt".split(None, 9)
>
> ['-rwx------', '1', 'user', 'group', '0', 'Feb', '04', '15:57', 'New',
>
> 'Text Document.txt']
>
>
>
> Therefore:
>
>
>
> for entry in ftp.nlst():
>
> if entry.endswith(".txt"):
>
> filename = entry.split(None, 9)[-1]
>
> ftp.delete(filename)
wow, thank you very much thats a huge help.
Chris
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
ftp - delete multiple files of same type chris.annin@gmail.com - 2013-02-05 09:29 -0800
Re: ftp - delete multiple files of same type Dave Angel <davea@davea.name> - 2013-02-05 13:24 -0500
Re: ftp - delete multiple files of same type MRAB <python@mrabarnett.plus.com> - 2013-02-05 18:45 +0000
Re: ftp - delete multiple files of same type chris.annin@gmail.com - 2013-02-05 14:59 -0800
Re: ftp - delete multiple files of same type chris.annin@gmail.com - 2013-02-05 14:59 -0800
csiph-web