Find and Delete all files with .xxx extension

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hokiegal99

    Find and Delete all files with .xxx extension

    The below code does what I need it to do, but I thought that using
    something like ext = os.path.splitex t(fname) and then searching ext[1]
    for '.mp3' would be a much more accurate approach to solving this
    problem. Could someone demonstrate how to search ext[1] for a specific
    string? This script works great for deleting illegal music on user
    machines ;)

    Thanks!!!


    import os, string
    setpath = raw_input("Ente r the path: ") #This can be hard coded.
    for root, dirs, files in os.walk(setpath , topdown=False):
    for fname in files:
    s = string.find(fna me, '.mp3')
    if s >=1:
    fpath = os.path.join(ro ot,fname)
    os.remove(fpath )
    print "Removed", fpath, "\n"

  • Georgy Pruss

    #2
    Re: Find and Delete all files with .xxx extension


    "hokiegal99 " <hokiegal99@hot mail.com> wrote in message news:3FDBC1B8.4 010205@hotmail. com...
    | s = string.find(fna me, '.mp3')
    | if s >=1:

    I would say: if fname.lower().e ndswith( '.mp3' ):


    Comment

    • William Park

      #3
      Re: Find and Delete all files with .xxx extension

      hokiegal99 <hokiegal99@hot mail.com> wrote:[color=blue]
      > The below code does what I need it to do, but I thought that using
      > something like ext = os.path.splitex t(fname) and then searching ext[1]
      > for '.mp3' would be a much more accurate approach to solving this
      > problem. Could someone demonstrate how to search ext[1] for a specific
      > string? This script works great for deleting illegal music on user
      > machines ;)
      >
      > Thanks!!!
      >
      >
      > import os, string
      > setpath = raw_input("Ente r the path: ") #This can be hard coded.
      > for root, dirs, files in os.walk(setpath , topdown=False):
      > for fname in files:
      > s = string.find(fna me, '.mp3')
      > if s >=1:
      > fpath = os.path.join(ro ot,fname)
      > os.remove(fpath )
      > print "Removed", fpath, "\n"[/color]

      FYI, in shell, you would go
      find . -type f -name '*.mp3' | xargs rm

      --
      William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
      Linux solution for data management and processing.

      Comment

      • Fredrik Lundh

        #4
        Re: Find and Delete all files with .xxx extension

        "hokiegal99 " wrote:[color=blue]
        > The below code does what I need it to do, but I thought that using
        > something like ext = os.path.splitex t(fname) and then searching ext[1]
        > for '.mp3' would be a much more accurate approach to solving this
        > problem. Could someone demonstrate how to search ext[1] for a specific
        > string?[/color]

        to quote myself from an earlier reply to you:

        os.path.splitex t(fname) splits a filename into prefix and extension
        parts:
        [color=blue][color=green][color=darkred]
        >>> os.path.splitex t("hello")[/color][/color][/color]
        ('hello', '')[color=blue][color=green][color=darkred]
        >>> os.path.splitex t("hello.doc" )[/color][/color][/color]
        ('hello', '.doc')[color=blue][color=green][color=darkred]
        >>> os.path.splitex t("hello.DOC" )[/color][/color][/color]
        ('hello', '.DOC')[color=blue][color=green][color=darkred]
        >>> os.path.splitex t("hello.foo" )[/color][/color][/color]
        ('hello', '.foo')

        in other words, ext[1] *is* the extension. if you want to look for mp3's,
        just compare the seconrd part to the string ".mp3":

        for fname in files:
        name, ext = os.path.splitex t(fname)
        if ext == ".mp3":
        # ... do something ...

        if you want to look for mp3, MP3, Mp3, etc, you can use the "lower"
        method on the extension:

        for fname in files:
        name, ext = os.path.splitex t(fname)
        ext = ext.lower()
        if ext == ".mp3":
        # ... do something with mp3 files ...
        if ext == ".foo":
        # ... do something with foo files ...

        </F>




        Comment

        • hokiegal99

          #5
          Re: Find and Delete all files with .xxx extension

          "Georgy Pruss" <see_signature_ _@hotmail.com> wrote in message news:<CvQCb.136 552$dl.5520824@ twister.southea st.rr.com>...[color=blue]
          > "hokiegal99 " <hokiegal99@hot mail.com> wrote in message news:3FDBC1B8.4 010205@hotmail. com...
          > | s = string.find(fna me, '.mp3')
          > | if s >=1:
          >
          > I would say: if fname.lower().e ndswith( '.mp3' ):[/color]

          Why the '.lower()' part? Wouldn't if fname.endswith( '.mp3'): work just
          as well, or am I missing something here?

          Comment

          • hokiegal99

            #6
            Re: Find and Delete all files with .xxx extension

            "Fredrik Lundh" <fredrik@python ware.com> wrote
            [color=blue]
            > to quote myself from an earlier reply to you:
            >
            > os.path.splitex t(fname) splits a filename into prefix and extension
            > parts:
            >[color=green][color=darkred]
            > >>> os.path.splitex t("hello")[/color][/color]
            > ('hello', '')[color=green][color=darkred]
            > >>> os.path.splitex t("hello.doc" )[/color][/color]
            > ('hello', '.doc')[color=green][color=darkred]
            > >>> os.path.splitex t("hello.DOC" )[/color][/color]
            > ('hello', '.DOC')[color=green][color=darkred]
            > >>> os.path.splitex t("hello.foo" )[/color][/color]
            > ('hello', '.foo')
            >
            > in other words, ext[1] *is* the extension.[/color]

            Yes, I know that. You helped me to understand that in a earlier,
            different question.

            [color=blue]
            > if you want to look for mp3, MP3, Mp3, etc, you can use the "lower"
            > method on the extension:
            >
            > for fname in files:
            > name, ext = os.path.splitex t(fname)
            > ext = ext.lower()
            > if ext == ".mp3":
            > # ... do something with mp3 files ...
            > if ext == ".foo":
            > # ... do something with foo files ...[/color]

            Thank you for this example, it's exactly what I was thinking of. I
            didn't know that I could use an equivalent comparison to determine
            whether or not ext[1] contained the string I wanted to remove, that's
            all I was asking.

            After reading over the various replies and testing them, I think that
            this is the best solution:

            if fname.lower().e ndswith('.mp3') :
            remove...

            Thanks again for the examples.

            Comment

            • Mark McEahern

              #7
              Re: Find and Delete all files with .xxx extension

              On Sun, 2003-12-14 at 09:06, hokiegal99 wrote:[color=blue][color=green]
              > > I would say: if fname.lower().e ndswith( '.mp3' ):[/color]
              >
              > Why the '.lower()' part? Wouldn't if fname.endswith( '.mp3'): work just
              > as well, or am I missing something here?[/color]

              Because the filename might be WHATEVER.MP3 and endswith, presumably, is
              case-sensitive.

              Cheers,

              // m


              Comment

              • Heike C. Zimmerer

                #8
                Re: Find and Delete all files with .xxx extension

                William Park <opengeometry@y ahoo.ca> writes:
                [color=blue]
                > hokiegal99 <hokiegal99@hot mail.com> wrote:[color=green]
                >> import os, string
                >> setpath = raw_input("Ente r the path: ") #This can be hard coded.
                >> for root, dirs, files in os.walk(setpath , topdown=False):
                >> for fname in files:
                >> s = string.find(fna me, '.mp3')
                >> if s >=1:
                >> fpath = os.path.join(ro ot,fname)
                >> os.remove(fpath )
                >> print "Removed", fpath, "\n"[/color]
                >
                > FYI, in shell, you would go
                > find . -type f -name '*.mp3' | xargs rm[/color]

                Which will fail if the file name contains any spaces or other special
                characters (not too unusual for .mp3 - Files).

                - Heike

                Comment

                • William Park

                  #9
                  Re: Find and Delete all files with .xxx extension

                  Heike C. Zimmerer <usenet03q2@hcz im.de> wrote:[color=blue]
                  > William Park <opengeometry@y ahoo.ca> writes:
                  >[color=green]
                  > > hokiegal99 <hokiegal99@hot mail.com> wrote:[color=darkred]
                  > >> import os, string
                  > >> setpath = raw_input("Ente r the path: ") #This can be hard coded.
                  > >> for root, dirs, files in os.walk(setpath , topdown=False):
                  > >> for fname in files:
                  > >> s = string.find(fna me, '.mp3')
                  > >> if s >=1:
                  > >> fpath = os.path.join(ro ot,fname)
                  > >> os.remove(fpath )
                  > >> print "Removed", fpath, "\n"[/color]
                  > >
                  > > FYI, in shell, you would go
                  > > find . -type f -name '*.mp3' | xargs rm[/color]
                  >
                  > Which will fail if the file name contains any spaces or other special
                  > characters (not too unusual for .mp3 - Files).[/color]

                  In which case, you look up 'man find xargs' and edit the command to
                  find ... -print0 | xargs -0 ...

                  --
                  William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
                  Linux solution for data management and processing.

                  Comment

                  • hokiegal99

                    #10
                    Re: Find and Delete all files with .xxx extension

                    William Park <opengeometry@y ahoo.ca> wrote in message news:<brntu1$5c 6a8$1@ID-99293.news.uni-berlin.de>...[color=blue]
                    > Heike C. Zimmerer <usenet03q2@hcz im.de> wrote:[color=green]
                    > > William Park <opengeometry@y ahoo.ca> writes:
                    > >[color=darkred]
                    > > > hokiegal99 <hokiegal99@hot mail.com> wrote:
                    > > >> import os, string
                    > > >> setpath = raw_input("Ente r the path: ") #This can be hard coded.
                    > > >> for root, dirs, files in os.walk(setpath , topdown=False):
                    > > >> for fname in files:
                    > > >> s = string.find(fna me, '.mp3')
                    > > >> if s >=1:
                    > > >> fpath = os.path.join(ro ot,fname)
                    > > >> os.remove(fpath )
                    > > >> print "Removed", fpath, "\n"
                    > > >
                    > > > FYI, in shell, you would go
                    > > > find . -type f -name '*.mp3' | xargs rm[/color]
                    > >
                    > > Which will fail if the file name contains any spaces or other special
                    > > characters (not too unusual for .mp3 - Files).[/color]
                    >
                    > In which case, you look up 'man find xargs' and edit the command to
                    > find ... -print0 | xargs -0 ...[/color]

                    What would you man on a Windows box??? Not everyone has a unix shell,
                    and not everyone wants/needs one. None of our users use Linux/Unix.
                    They all use Windows XP or Mac OS X. So, you shouldn't assume that I'm
                    running Linux or some other type of Unix. I do as an admin/developer,
                    but that's not the point. The above python script will run on Windows,
                    Linux, OS X, etc. and it makes for much easier reading. This is a
                    python forum. So, you're off-topic.

                    Comment

                    • William Park

                      #11
                      Re: Find and Delete all files with .xxx extension

                      hokiegal99 <hokiegal99@hot mail.com> wrote:[color=blue][color=green]
                      > > In which case, you look up 'man find xargs' and edit the command to
                      > > find ... -print0 | xargs -0 ...[/color]
                      >
                      > What would you man on a Windows box??? Not everyone has a unix shell,
                      > and not everyone wants/needs one. None of our users use Linux/Unix.
                      > They all use Windows XP or Mac OS X. So, you shouldn't assume that I'm
                      > running Linux or some other type of Unix. I do as an admin/developer,
                      > but that's not the point. The above python script will run on Windows,
                      > Linux, OS X, etc. and it makes for much easier reading. This is a
                      > python forum. So, you're off-topic.[/color]

                      Sorry... old habit dies hard. It's just that most people here are on
                      the receiving end of paycheck, whereas I am on the other end. And, I
                      find it hard to write a cheque for 3 days of work for something that can
                      be done with one line of code.

                      --
                      William Park, Open Geometry Consulting, <opengeometry@y ahoo.ca>
                      Linux solution for data management and processing.

                      Comment

                      • Derrick 'dman' Hudson

                        #12
                        Re: Find and Delete all files with .xxx extension

                        On 16 Dec 2003 19:12:34 -0800, hokiegal99 wrote:[color=blue]
                        > William Park <opengeometry@y ahoo.ca> wrote in message news:<brntu1$5c 6a8$1@ID-99293.news.uni-berlin.de>...[color=green]
                        >> Heike C. Zimmerer <usenet03q2@hcz im.de> wrote:[color=darkred]
                        >> > William Park <opengeometry@y ahoo.ca> writes:[/color][/color][/color]
                        [color=blue][color=green][color=darkred]
                        >> > > FYI, in shell, you would go
                        >> > > find . -type f -name '*.mp3' | xargs rm
                        >> >
                        >> > Which will fail if the file name contains any spaces or other special
                        >> > characters (not too unusual for .mp3 - Files).[/color]
                        >>
                        >> In which case, you look up 'man find xargs' and edit the command to
                        >> find ... -print0 | xargs -0 ...[/color]
                        >
                        > What would you man on a Windows box???[/color]

                        Start by clicking
                        Start -> Programs -> Cygwin -> Bash Shell

                        ;-)
                        [color=blue]
                        > Not everyone has a unix shell,[/color]

                        True.
                        [color=blue]
                        > and not everyone wants/needs one.[/color]

                        True too.
                        [color=blue]
                        > None of our users use Linux/Unix.
                        > They all use Windows XP[/color]

                        That means you are stuck writing simple system management programs for
                        them because the system doesn't have them out-of-the-box. It's your
                        [users'] choice.
                        [color=blue]
                        > or Mac OS X.[/color]

                        OSX comes with a POSIX shell.
                        [color=blue]
                        > So, you shouldn't assume that I'm running Linux or some other type
                        > of Unix.[/color]

                        But you are :-). OSX is "some other type of Unix".
                        [color=blue]
                        > I do as an admin/developer, but that's not the point.[/color]

                        Agreed.
                        [color=blue]
                        > The above python script will run on Windows, Linux, OS X, etc.[/color]

                        Yeah, yeah. (for such a simple task I'm not impressed, but for
                        non-trivial stuff (eg more complex "delete all files matching certain
                        criteria) and the like I would be)
                        [color=blue]
                        > and it makes for much easier reading.[/color]

                        I think the find(1) example is quite easy reading. It's one compact
                        line, with all the detailed file handling taken care of automatically.
                        [color=blue]
                        > This is a python forum. So, you're off-topic.[/color]

                        Not really off-topic. Many times a someone relatively new and/or
                        inexperienced with computers posts a system admin type of question in
                        this forum not knowing that the tools already exist. Pointing out
                        some existing tools, rather than encouraging reinvention, is never a
                        bad thing. Sometimes that education is invaluable for the OP or even
                        for other lurkers. If the given solution doesn't apply to your
                        environment, kindly pass on to the next suggestion or (nicely :-))
                        clarify if need be.

                        -D

                        --
                        [Perl] combines all the worst aspects of C and Lisp: a billion different
                        sublanguages in one monolithic executable.
                        It combines the power of C with the readability of PostScript.
                        -- Jamie Zawinski

                        www: http://dman13.dyndns.org/~dman/ jabber: dman@dman13.dyn dns.org

                        Comment

                        • Thomas Heller

                          #13
                          Re: Find and Delete all files with .xxx extension

                          Derrick 'dman' Hudson <dman@dman13.dy ndns.org> writes:
                          [color=blue]
                          > On 16 Dec 2003 19:12:34 -0800, hokiegal99 wrote:[color=green]
                          >> William Park <opengeometry@y ahoo.ca> wrote in message news:<brntu1$5c 6a8$1@ID-99293.news.uni-berlin.de>...[color=darkred]
                          >>> Heike C. Zimmerer <usenet03q2@hcz im.de> wrote:
                          >>> > William Park <opengeometry@y ahoo.ca> writes:[/color][/color]
                          >[color=green][color=darkred]
                          >>> > > FYI, in shell, you would go
                          >>> > > find . -type f -name '*.mp3' | xargs rm
                          >>> >
                          >>> > Which will fail if the file name contains any spaces or other special
                          >>> > characters (not too unusual for .mp3 - Files).
                          >>>
                          >>> In which case, you look up 'man find xargs' and edit the command to
                          >>> find ... -print0 | xargs -0 ...[/color]
                          >>
                          >> What would you man on a Windows box???[/color]
                          >
                          > Start by clicking
                          > Start -> Programs -> Cygwin -> Bash Shell[/color]

                          No, enter 'del /s *.mp3' at the command prompt.
                          If you are running windows, you should know it.

                          Comment

                          Working...