ftp - delete multiple files of same type

Discussion in 'Python' started by chris.annin@gmail.com, Feb 5, 2013.

  1. Guest

    im trying to delete all text files from an ftp directory. is there a way todelete 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()
    any ideas on this? thank you.
     
    , Feb 5, 2013
    #1
    1. Advertising

  2. Dave Angel Guest

    On 02/05/2013 12:29 PM, 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()
    >
    > 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
     
    Dave Angel, Feb 5, 2013
    #2
    1. Advertising

  3. MRAB Guest

    On 2013-02-05 17:29, 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()
    >
    > 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)
     
    MRAB, Feb 5, 2013
    #3
  4. Guest


    >
    > > 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 thedate 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 shouldbe a better more bullet proof way to do this?

    >
    > >

    >
    > >
    Code:
    import os[/color]
    > [color=green]
    > > import system[/color]
    > [color=green]
    > > from ftplib import FTP[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp = FTP('127.0.0.1')[/color]
    > [color=green]
    > > ftp.login('')[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > directory = 'test'[/color]
    > [color=green]
    > > ftp.cwd(directory)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > files = ftp.nlst()[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > for file in files:[/color]
    > [color=green]
    > >      if file.find(".txt") != -1:[/color]
    > [color=green]
    > >          file = (file [-21:])[/color]
    > [color=green]
    > >          ftp.delete(file)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp.close()

    >
    > >

    >
    > > 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, 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 thedate 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 shouldbe a better more bullet proof way to do this?

    >
    > >

    >
    > >
    Code:
    import os[/color]
    > [color=green]
    > > import system[/color]
    > [color=green]
    > > from ftplib import FTP[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp = FTP('127.0.0.1')[/color]
    > [color=green]
    > > ftp.login('')[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > directory = 'test'[/color]
    > [color=green]
    > > ftp.cwd(directory)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > files = ftp.nlst()[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > for file in files:[/color]
    > [color=green]
    > >      if file.find(".txt") != -1:[/color]
    > [color=green]
    > >          file = (file [-21:])[/color]
    > [color=green]
    > >          ftp.delete(file)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp.close()

    >
    > >

    >
    > > 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
     
    , Feb 5, 2013
    #4
  5. Guest


    >
    > > 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 thedate 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 shouldbe a better more bullet proof way to do this?

    >
    > >

    >
    > >
    Code:
    import os[/color]
    > [color=green]
    > > import system[/color]
    > [color=green]
    > > from ftplib import FTP[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp = FTP('127.0.0.1')[/color]
    > [color=green]
    > > ftp.login('')[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > directory = 'test'[/color]
    > [color=green]
    > > ftp.cwd(directory)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > files = ftp.nlst()[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > for file in files:[/color]
    > [color=green]
    > >      if file.find(".txt") != -1:[/color]
    > [color=green]
    > >          file = (file [-21:])[/color]
    > [color=green]
    > >          ftp.delete(file)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp.close()

    >
    > >

    >
    > > 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, 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 thedate 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 shouldbe a better more bullet proof way to do this?

    >
    > >

    >
    > >
    Code:
    import os[/color]
    > [color=green]
    > > import system[/color]
    > [color=green]
    > > from ftplib import FTP[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp = FTP('127.0.0.1')[/color]
    > [color=green]
    > > ftp.login('')[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > directory = 'test'[/color]
    > [color=green]
    > > ftp.cwd(directory)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > files = ftp.nlst()[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > for file in files:[/color]
    > [color=green]
    > >      if file.find(".txt") != -1:[/color]
    > [color=green]
    > >          file = (file [-21:])[/color]
    > [color=green]
    > >          ftp.delete(file)[/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > ftp.close()

    >
    > >

    >
    > > 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
     
    , Feb 5, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Biuri
    Replies:
    0
    Views:
    426
    Biuri
    Jul 21, 2003
  2. Replies:
    4
    Views:
    974
    M.E.Farmer
    Feb 13, 2005
  3. Drew
    Replies:
    1
    Views:
    658
  4. pilgrim773
    Replies:
    2
    Views:
    1,873
    Giampaolo Rodolà
    May 19, 2010
  5. D. Buck
    Replies:
    2
    Views:
    534
    D. Buck
    Jun 29, 2004
Loading...

Share This Page