简体   繁体   English

Python - 删除包含特定字符的文件

[英]Python - Delete a file with a certain character in it

I have a lot of duplicate files in a folder and I would like to delete the duplicate.我的文件夹中有很多重复文件,我想删除重复文件。 As of now i have FileA.jpg and FileA(1).jpg .截至目前,我有FileA.jpgFileA(1).jpg I would like to make a short script that open a directory and finds any file name that has a ( and then delete it.我想制作一个简短的脚本来打开一个目录并找到任何具有(然后删除它的文件名。

How would I do this?我该怎么做?

You can use OS package.您可以使用OS package。

import os

for filePath in os.listdir("/path/to/dir"):
    if "(" in filePath:
        os.remove(filePath)

This is one of the ways how it can be done (deleting files only):这是可以完成的方法之一(仅删除文件):

from os import listdir
import os
from os.path import isfile, join

PATH_CONST = r'\temp'
myFiles = [f for f in listdir(PATH_CONST) if isfile(join(PATH_CONST, f))]

print(myFiles)

for file in myFiles:
    if '(' in file:
        os.remove(PATH_CONST+r'\\'+file)

I'd use a regex for this personally:我个人会为此使用正则表达式

.*\(\d+\)

Code example (with re module)代码示例(带re模块)

import os
import re


is_dupe_file = re.compile(r'\(\d+\)').search

for filePath in os.listdir("/path/to/dir"):
    if is_dupe_file(filePath):
        print('matches:', filePath)

For test purposes, I recommend to use a list with a few dummy filenames:出于测试目的,我建议使用带有一些虚拟文件名的列表:

files = r"""
FileA(1).jpg
hello/FileA(33).jpg
FileA.jpg
FileB().jpg
hi\there\File1(test).jpg
FileB(a).jpg
FileB(23)
FileB(1234567).jpg
""".strip().split('\n')

for filePath in files:
    if is_dupe_file(filePath):
        print('matches:', filePath)

Out:出去:

matches: FileA(1).jpg
matches: hello/FileA(33).jpg
matches: FileB(23)
matches: FileB(1234567).jpg

You can do this with the os package So first import os, then get the cwd or current working directory.您可以使用 os package 执行此操作 所以首先导入 os,然后获取 cwd 或当前工作目录。 This means whichever folder the python script is in, it will perform all the code for this specific folder.这意味着无论 python 脚本位于哪个文件夹,它将执行此特定文件夹的所有代码。 For instance, you put this.py file in the folder with all the duplicate files.例如,您将 this.py 文件放在包含所有重复文件的文件夹中。

import os
cwd = os.getcwd()

#print all files in cwd
for file in os.listdir(cwd):
    if "(1)" in file: 
        print("Duplicate file found: " + file)
        os.remove(file)
    else:
        continue

You can change the (1) to whatever you want to search for.您可以将 (1) 更改为您想要搜索的任何内容。 If you have more than one duplicate of each image, meaning it goes above 1, then just search for files with "(" in the name for example. I also added a print so that it tells you in the console which file is being deleted.如果每个图像有多个副本,这意味着它超过 1,那么只需搜索名称中带有“(”的文件。我还添加了一个打印,以便它在控制台中告诉您哪个文件正在被删除.

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM