Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38207 > unrolled thread
| Started by | chris.annin@gmail.com |
|---|---|
| First post | 2013-02-05 09:29 -0800 |
| Last post | 2013-02-05 14:59 -0800 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | chris.annin@gmail.com |
|---|---|
| Date | 2013-02-05 09:29 -0800 |
| Subject | ftp - delete multiple files of same type |
| Message-ID | <1d7b7a3d-097f-477b-93b5-4343a25bc1f1@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-02-05 13:24 -0500 |
| Message-ID | <mailman.1377.1360088678.2939.python-list@python.org> |
| In reply to | #38207 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-02-05 18:45 +0000 |
| Message-ID | <mailman.1378.1360089902.2939.python-list@python.org> |
| In reply to | #38207 |
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)
[toc] | [prev] | [next] | [standalone]
| From | chris.annin@gmail.com |
|---|---|
| Date | 2013-02-05 14:59 -0800 |
| Message-ID | <bd5a8b02-9672-4f23-af99-964ea028f208@googlegroups.com> |
| In reply to | #38215 |
>
> > 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
[toc] | [prev] | [next] | [standalone]
| From | chris.annin@gmail.com |
|---|---|
| Date | 2013-02-05 14:59 -0800 |
| Message-ID | <mailman.1395.1360105160.2939.python-list@python.org> |
| In reply to | #38215 |
>
> > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web