简体   繁体   中英

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 . 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.

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)

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. This means whichever folder the python script is in, it will perform all the code for this specific folder. For instance, you put this.py file in the folder with all the duplicate files.

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. 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.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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