删除给定类型的文件 IFF 文件夹中没有其他文件类型

发布于 2024-12-09 01:35:09 字数 518 浏览 0 评论 0原文

我想删除 [cue,jpg,png,m3u,etc.] 类型的某些文件,但当且仅当它们本身位于文件夹中或 [cue,jpg,png,m3u,etc.] 类型的其他文件中时。我已经有一个函数可以获取任何给定文件夹中所有 [cue、jpg、png、m3u 等] 类型的文件并将其作为列表返回,但我只需要根据条件删除所有文件多于。举个例子:文件 q.jpg 单独位于一个文件夹中。 myfunc() 完成后,它将被删除。

编辑 抱歉不清楚。让我举一个更好的例子: 我们有两个文件夹,AlphaBeta,位于Gamma 文件夹中。在Alpha中,有三个文件:1.mp3、2.mp3、folder.jpg。在Beta中,有一个文件cover.jpgmyfunc() 完成后,1.mp3、2.mp3、folder.jpg 应保持不变,而 cover.jpg 应被删除。

I want to delete certain files of type [cue,jpg,png,m3u,etc.] but if and only if they are in a folder by themselves or other files of [cue,jpg,png,m3u,etc.] type. I already have a function that can get me all the files of type [cue,jpg,png,m3u,etc.] in any given folder and return it as a list, but I just need to delete all of it according to the conditions above. To give an example: File q.jpg is in a folder by itself. After myfunc() finishes, it is deleted.

EDIT Sorry for unclearness. Let me give a better example:
We have two folders, Alpha and Beta in folder Gamma. In Alpha, there are three files: 1.mp3, 2.mp3, folder.jpg. In Beta, there is one file cover.jpg. After myfunc() finishes, 1.mp3, 2.mp3, folder.jpg should be untouched while cover.jpg should be deleted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-12-16 01:35:09

听起来您有两个步骤:

1)给定扩展名列表,获取与该扩展名匹配的文件夹中的所有文件的列表。

2)如果目录中的所有文件都有与您的列表匹配的扩展名,请删除它们

请注意,这不包括有关如何遍历目录结构或要测试哪些目录的任何信息...示例代码有一个目录硬编码在.

import os       

dir = "myDirectory" 
extList = ['ext1', 'ext2', 'ext3']
allfiles = os.listdir(dir) # all files in that directory

myfiles = [] # will be appended to to only contain files with extensiosn matching extlist
for file in allfiles:
    parts = file.split('.') # split the filename based on .
    if parts[-1] in extensionlist:
        myfiles.append(file) 

if len(myfiles) == len(allfiles):
    for file in myfiles:
        path = "%s/%s" % (dir, file)
        os.remove(path)
        os.remove(file)

It sounds like you have two steps:

1) Given a list of extensions, get the list of all files in a folder matching that extension.

2) If all the files in your directory have an extension matching your list, remove them

Note that this doesn't include any information about how to traverse the directory structure, or which directories to test ... the sample code has a single directory hard-coded in.

import os       

dir = "myDirectory" 
extList = ['ext1', 'ext2', 'ext3']
allfiles = os.listdir(dir) # all files in that directory

myfiles = [] # will be appended to to only contain files with extensiosn matching extlist
for file in allfiles:
    parts = file.split('.') # split the filename based on .
    if parts[-1] in extensionlist:
        myfiles.append(file) 

if len(myfiles) == len(allfiles):
    for file in myfiles:
        path = "%s/%s" % (dir, file)
        os.remove(path)
        os.remove(file)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文