文件名称是递增的,删除掉其中一部分的文件,导致递增文件缺少,通过python查找出缺少的递增文件名称

文件名称是递增的,删除掉其中一部分的文件,导致递增文件缺少,通过python查找出缺少的递增文件名称

文件名称是递增的,删除掉其中一部分的文件,导致递增文件缺少,通过python查找出缺少的递增文件名称_第1张图片

通过python,查找出缺少的递增的文件的名称:

在这里插入图片描述

代码如下:

import os

def find_missing_numbers(folder_path):
    file_list = os.listdir(folder_path)
    sorted_files = sorted(file_list)

    missing_numbers = []
    prev_number = None
    
    for file_name in sorted_files:
        # 提取文件名中的数字部分
        number_str = file_name.split("_")[-1].split(".")[0]

        if number_str.isdigit():
            current_number = int(number_str)
            
            if prev_number is not None and current_number > prev_number + 1:
                # 发现缺失的递增数字
                for i in range(prev_number + 1, current_number):
                    missing_numbers.append(i)

            prev_number = current_number
    
    return missing_numbers

# 指定要查找的文件夹路径 此处放入你要访问的文件夹
folder_path = '/path/to/your/folder'

missing_nums = find_missing_numbers(folder_path)
if len(missing_nums) == 0:
    print("没有发现缺失的递增数字")
else:
    print("发现缺失的递增数字:", missing_nums)

注意:我的文件是前面数字中间有个“—”,后面的数字递增,你可以稍微修改一下代码就可以用

import os

def find_missing_numbers(folder_path):
    file_list = os.listdir(folder_path)
    sorted_files = sorted(file_list)

    missing_numbers = []
    prev_number = None
    
    for file_name in sorted_files:
        # 提取文件名中的数字部分
        number_str = file_name.split("_")[-1]

        if number_str.isdigit():
            current_number = int(number_str)
            
            if prev_number is not None and current_number > prev_number + 1:
                # 发现缺失的递增数字
                for i in range(prev_number + 1, current_number):
                    missing_numbers.append(i)

            prev_number = current_number
    
    return missing_numbers

# 指定要查找的文件夹路径
# folder_path = '/path/to/your/folder'
folder_path = "E:/python/yoloProjectTestSmallLarge/data/VOC2028/Annotations"

missing_nums = find_missing_numbers(folder_path)
if len(missing_nums) == 0:
    print("没有发现缺失的递增数字")
else:
    print("发现缺失的递增数字:", missing_nums)

担心有些不知道怎么改,我将原始不带“_”的代码奉上

import os

def find_missing_numbers(folder_path):
    file_list = os.listdir(folder_path)
    sorted_files = sorted(file_list)

    missing_numbers = []
    prev_number = None
    
    for file_name in sorted_files:
        # 提取文件名中的数字部分
        number_str = ''.join(filter(str.isdigit, file_name))

        if number_str != '':
            current_number = int(number_str)
            
            if prev_number is not None and current_number > prev_number + 1:
                # 发现缺失的递增数字
                for i in range(prev_number + 1, current_number):
                    missing_numbers.append(i)

            prev_number = current_number
    
    return missing_numbers

# 指定要查找的文件夹路径
folder_path = '/path/to/your/folder'

missing_nums = find_missing_numbers(folder_path)
if len(missing_nums) == 0:
    print("没有发现缺失的递增数字")
else:
    print("发现缺失的递增数字:", missing_nums)

你可能感兴趣的:(python,python,windows,linux)