i have a directory of files with namings as following : Binance1INCHBUSD1h…
- User
i have a directory of files with namings as following :
Binance_1INCHBUSD_1h Binance_1INCHDOWNUSDT_1h Binance_AAVEBKRW_1h day_Binance_ACMUSDT_1h day_Binance_ADABKRW_1h and...
i want to check this list of files to find duplicate items based on 1INCHBUSD part of names (name.split('_')[-2]) give me the proper python code
- Assistant
To find duplicates based on the part of the filename you are interested in (the part before the last underscore), you can use a Python script like the one below. This script assumes the filenames are stored in a list, but you can easily adapt it to read filenames from a directory using libraries such as
osorglob.Here is a python script that finds duplicates based on your criteria:
from collections import defaultdict # Example list of filenames filenames = [ "Binance_1INCHBUSD_1h", "Binance_1INCHDOWNUSDT_1h", "Binance_AAVEBKRW_1h", "day_Binance_ACMUSDT_1h", "day_Binance_ADABKRW_1h", # Add more filenames as needed ] # Dictionary to store the count of each unique part based on your criteria part_count = defaultdict(int) # Fill the dictionary for filename in filenames: # Splitting filename and getting the part based on your criteria unique_part = filename.split('_')[-2] part_count[unique_part] += 1 # Finding duplicates duplicates = [part for part, count in part_count.items() if count > 1] print("Duplicates based on the unique part:") for duplicate in duplicates: print(duplicate) - User
i also want to rename my files to a desired naming
- Assistant