查找并删除扩展名为.xxx的所有文件 [英] Find and Delete all files with .xxx extension

查看:81
本文介绍了查找并删除扩展名为.xxx的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码完成了我需要它做的事情,但我认为使用

类似于ext = os.path.splitext(fname)然后搜索ext [1]
.mp3的
将是解决这个问题的更准确的方法。有人可以演示如何在ext [1]中搜索特定的

字符串吗?这个脚本非常适合删除用户的非法音乐

机器;)


谢谢!!!

import os,string

setpath = raw_input("输入路径:")#这可以是硬编码的。

for root,dirs,os.walk中的文件(setpath,topdown =错误):

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"

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"

推荐答案



" hokiegal99" <豪******** @ hotmail.com>在留言新闻中写道:3F ************** @ hotmail.com ...

| s = string.find(fname,''。mp3'')

|如果s> = 1:


我会说:如果fname.lower()。endswith(''。mp3''):

"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'' ):


hokiegal99< ho ******** @ hotmail.com>写道:
hokiegal99 <ho********@hotmail.com> wrote:
下面的代码完成了我需要它做的事情,但我认为使用
类似于ext = os.path.splitext(fname)然后搜索ext [1] <对于''.mp3''来说,解决这个问题会更精确。有人可以演示如何在ext [1]中搜索特定的字符串吗?这个脚本非常适合删除用户机器上的非法音乐;)

谢谢!!!

import os,string
setpath = raw_input(" ;输入路径:")#这可以是硬编码的。
对于root,dirs,os.walk中的文件(setpath,topdown = False):
对于文件中的fname:
s = string.find(fname,''。mp3'')
如果s> = 1:
fpath = os.path.join(root,fname)
os.remove( fpath)
print" Removed",fpath," \ n"
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,在shell中,你会去

找到。 -type f -name''* .mp3''| xargs rm


-

William Park,Open Geometry Consulting,< op ********** @ yahoo.ca>

用于数据管理和处理的Linux解决方案。



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.


" hokiegal99"写道:
"hokiegal99" wrote:
下面的代码完成了我需要它做的事情,但我认为使用
类似于ext = os.path.splitext(fname)然后搜索ext [1] <对于''.mp3''来说,解决这个问题会更精确。有人可以演示如何在ext [1]中搜索特定的字符串吗?
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?




引用自己之前的回复:


os.path.splitext(fname)将文件名拆分为前缀和扩展名

部分:



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")
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'')


换句话说,ext [1] *是*的扩展名。如果你想查找mp3',

只需将seconrd部分与文件中的fname的字符串.mp3:


进行比较:

name,ext = os.path.splitext(fname)

如果ext ==" .mp3":

#...做点什么...


如果你想寻找mp3,MP3,Mp3等,你可以使用更低的

方法扩展名:


for fname in files:

name,ext = os.path.splitext(fname)

ext = ext .lower()

如果ext ==" .mp3":

#...用mp3文件做点什么...

如果ext ==" .foo":

#...用foo文件做些什么...


< / F>



(''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>



这篇关于查找并删除扩展名为.xxx的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆