473,915 Members | 3,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"

Jul 18 '05 #1
12 7981

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

I would say: if fname.lower().e ndswith( '.mp3' ):
Jul 18 '05 #2
hokiegal99 <ho********@hot mail.com> wrote:
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"


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

--
William Park, Open Geometry Consulting, <op**********@y ahoo.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.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?


to quote myself from an earlier reply to you:

os.path.splitex t(fname) splits a filename into prefix and extension
parts:
os.path.splitex t("hello") ('hello', '') os.path.splitex t("hello.doc" ) ('hello', '.doc') os.path.splitex t("hello.DOC" ) ('hello', '.DOC') os.path.splitex t("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.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>


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

I would say: if fname.lower().e ndswith( '.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*****@python ware.com> wrote
to quote myself from an earlier reply to you:

os.path.splitex t(fname) splits a filename into prefix and extension
parts:
>>> os.path.splitex t("hello") ('hello', '') >>> os.path.splitex t("hello.doc" ) ('hello', '.doc') >>> os.path.splitex t("hello.DOC" ) ('hello', '.DOC') >>> os.path.splitex t("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.splitex t(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().e ndswith('.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().e ndswith( '.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**********@y ahoo.ca> writes:
hokiegal99 <ho********@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


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********@hcz im.de> wrote:
William Park <op**********@y ahoo.ca> writes:
hokiegal99 <ho********@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


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**********@y ahoo.ca>
Linux solution for data management and processing.
Jul 18 '05 #9
William Park <op**********@y ahoo.ca> wrote in message news:<br******* *****@ID-99293.news.uni-berlin.de>...
Heike C. Zimmerer <us********@hcz im.de> wrote:
William Park <op**********@y ahoo.ca> writes:
hokiegal99 <ho********@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


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3164
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 (xxx.sql) each night and only want to keep the last 2 or three of them around. This will also be a cron job (I know how to run a php script in cron.)
16
17051
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 must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
4
11898
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 older. I was able to figure out how to delete them if they are 5 days older then current date but the weekends and holiday, etc. leave some undeleted. Any help will be greatly appreciated, ShyGuy
3
20099
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
1353
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
2274
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 directory, which name contains that string. Here is how I open the file of a directory. How can it be modified to
4
1711
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 of them have games. It was the department policy that no games must be installed in each PC. Can you guys help me with this? or can you modify this VBscript? dim strExcludedPC ServerFileSave="\\S1-admin-06\User Area\Save\" strComputer = "."...
2
24122
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT ) ); I delete all files, which older than 5 days and I use a background thread.
1
2210
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 would delete all the files regardless of how many there were, and regardless of their file
0
9883
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11359
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8102
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7259
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3370
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.