469,282 Members | 1,914 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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.splitext(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("Enter the path: ") #This can be hard coded.
for root, dirs, files in os.walk(setpath, topdown=False):
for fname in files:
s = string.find(fname, '.mp3')
if s >=1:
fpath = os.path.join(root,fname)
os.remove(fpath)
print "Removed", fpath, "\n"

Jul 18 '05 #1
12 7644

"hokiegal99" <ho********@hotmail.com> wrote in message news:3F**************@hotmail.com...
| s = string.find(fname, '.mp3')
| if s >=1:

I would say: if fname.lower().endswith( '.mp3' ):
Jul 18 '05 #2
hokiegal99 <ho********@hotmail.com> wrote:
The below code does what I need it to do, but I thought that using
something like ext = os.path.splitext(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("Enter the path: ") #This can be hard coded.
for root, dirs, files in os.walk(setpath, topdown=False):
for fname in files:
s = string.find(fname, '.mp3')
if s >=1:
fpath = os.path.join(root,fname)
os.remove(fpath)
print "Removed", fpath, "\n"


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

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #3
"hokiegal99" wrote:
The below code does what I need it to do, but I thought that using
something like ext = os.path.splitext(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?


to quote myself from an earlier reply to you:

os.path.splitext(fname) splits a filename into prefix and extension
parts:
os.path.splitext("hello") ('hello', '') os.path.splitext("hello.doc") ('hello', '.doc') os.path.splitext("hello.DOC") ('hello', '.DOC') os.path.splitext("hello.foo")

('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.splitext(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.splitext(fname)
ext = ext.lower()
if ext == ".mp3":
# ... do something with mp3 files ...
if ext == ".foo":
# ... do something with foo files ...

</F>


Jul 18 '05 #4
"Georgy Pruss" <se*************@hotmail.com> wrote in message news:<Cv*********************@twister.southeast.rr .com>...
"hokiegal99" <ho********@hotmail.com> wrote in message news:3F**************@hotmail.com...
| s = string.find(fname, '.mp3')
| if s >=1:

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


Why the '.lower()' part? Wouldn't if fname.endswith('.mp3'): work just
as well, or am I missing something here?
Jul 18 '05 #5
"Fredrik Lundh" <fr*****@pythonware.com> wrote
to quote myself from an earlier reply to you:

os.path.splitext(fname) splits a filename into prefix and extension
parts:
>>> os.path.splitext("hello") ('hello', '') >>> os.path.splitext("hello.doc") ('hello', '.doc') >>> os.path.splitext("hello.DOC") ('hello', '.DOC') >>> os.path.splitext("hello.foo")
('hello', '.foo')

in other words, ext[1] *is* the extension.


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

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.splitext(fname)
ext = ext.lower()
if ext == ".mp3":
# ... do something with mp3 files ...
if ext == ".foo":
# ... do something with foo files ...


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().endswith('.mp3'):
remove...

Thanks again for the examples.
Jul 18 '05 #6
On Sun, 2003-12-14 at 09:06, hokiegal99 wrote:
I would say: if fname.lower().endswith( '.mp3' ):


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


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

Cheers,

// m
Jul 18 '05 #7
William Park <op**********@yahoo.ca> writes:
hokiegal99 <ho********@hotmail.com> wrote:
import os, string
setpath = raw_input("Enter the path: ") #This can be hard coded.
for root, dirs, files in os.walk(setpath, topdown=False):
for fname in files:
s = string.find(fname, '.mp3')
if s >=1:
fpath = os.path.join(root,fname)
os.remove(fpath)
print "Removed", fpath, "\n"


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).

- Heike
Jul 18 '05 #8
Heike C. Zimmerer <us********@hczim.de> wrote:
William Park <op**********@yahoo.ca> writes:
hokiegal99 <ho********@hotmail.com> wrote:
import os, string
setpath = raw_input("Enter the path: ") #This can be hard coded.
for root, dirs, files in os.walk(setpath, topdown=False):
for fname in files:
s = string.find(fname, '.mp3')
if s >=1:
fpath = os.path.join(root,fname)
os.remove(fpath)
print "Removed", fpath, "\n"


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 ...

--
William Park, Open Geometry Consulting, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #9
William Park <op**********@yahoo.ca> wrote in message news:<br************@ID-99293.news.uni-berlin.de>...
Heike C. Zimmerer <us********@hczim.de> wrote:
William Park <op**********@yahoo.ca> writes:
hokiegal99 <ho********@hotmail.com> wrote:
> import os, string
> setpath = raw_input("Enter the path: ") #This can be hard coded.
> for root, dirs, files in os.walk(setpath, topdown=False):
> for fname in files:
> s = string.find(fname, '.mp3')
> if s >=1:
> fpath = os.path.join(root,fname)
> os.remove(fpath)
> print "Removed", fpath, "\n"

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 ...


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.
Jul 18 '05 #10
hokiegal99 <ho********@hotmail.com> wrote:
In which case, you look up 'man find xargs' and edit the command to
find ... -print0 | xargs -0 ...


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.


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, <op**********@yahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #11
On 16 Dec 2003 19:12:34 -0800, hokiegal99 wrote:
William Park <op**********@yahoo.ca> wrote in message news:<br************@ID-99293.news.uni-berlin.de>...
Heike C. Zimmerer <us********@hczim.de> wrote:
> William Park <op**********@yahoo.ca> writes: > > 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 ...


What would you man on a Windows box???


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

;-)
Not everyone has a unix shell,
True.
and not everyone wants/needs one.
True too.
None of our users use Linux/Unix.
They all use Windows XP
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.
or Mac OS X.
OSX comes with a POSIX shell.
So, you shouldn't assume that I'm running Linux or some other type
of Unix.
But you are :-). OSX is "some other type of Unix".
I do as an admin/developer, but that's not the point.
Agreed.
The above python script will run on Windows, Linux, OS X, etc.
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)
and it makes for much easier reading.
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.
This is a python forum. So, you're off-topic.


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: dm**@dman13.dyndns.org
Jul 18 '05 #12
Derrick 'dman' Hudson <dm**@dman13.dyndns.org> writes:
On 16 Dec 2003 19:12:34 -0800, hokiegal99 wrote:
William Park <op**********@yahoo.ca> wrote in message news:<br************@ID-99293.news.uni-berlin.de>...
Heike C. Zimmerer <us********@hczim.de> wrote:
> William Park <op**********@yahoo.ca> writes: > 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 ...


What would you man on a Windows box???


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


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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Adams-Blake Co. | last post: by
16 posts views Thread by Philip Boonzaaier | last post: by
3 posts views Thread by Krazitchek | last post: by
4 posts views Thread by Milsnips | last post: by
2 posts views Thread by graphicsxp | last post: by
2 posts views Thread by Thomas Bauer | last post: by
1 post views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.