473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 7921

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adams-Blake Co. | last post by:
Can this be done in PHP? I want to read a directory and for each file with a .sql extension that is older than 3 days, I want to delete it. I have a cron job that makes mysqldump files ...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
4
by: Shyguy | last post by:
I have a database that backs up critical tables to a database, named Backup with the date, daily. I can't figure out how to delete these databases programmatically, say when they are 5 days old or...
3
by: Krazitchek | last post by:
Hi, how do i do to delete files with a specific extension (like *.lnk). I try File.Delete(@"e:\test\\*.lnk) but it does not work, not the good way i guess... Help please, thanks.
4
by: Milsnips | last post by:
Can anyone help out on this one? i would like to find out all the ASPX and ASCX pages that are in my project, and return them in an arraylist. thanks, Paul.
2
by: graphicsxp | last post by:
Hi, How can I open all the files in a directory, which names match a particular string ? Say I have a string like 'a file name to find' and I want to find and open all the files of a given...
4
by: viper888 | last post by:
Hi to all, I'm the newly appointed network administrator in our office, and upon scanning all the PCs that are connected to the network, (by the way we're using a windows 2000 server) almost all...
2
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT )...
1
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a folder with the following files: a.doc a.csv a.pdf Sometimes all three files are there. Sometimes there is only one file there. Is there a File.Delete command that I can use that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.