Search code examples
pythonregexglobpython-itertoolspython-os

How to find unique files after removing files that have values in a particular list in Python?


I have the following files in a folder:

"ABC"
"ABC 10"
"ABC 22"
"ABC 30"
"ABC L1"
"ABC L2"
"ABC 10 L1"
"ABC 10 L2"
"ABC 22 L1"
"ABC 22 L2"
"ABC 30 L1"
"ABC 30 L2"
"PQR"
"PQR 10"
"PQR 22"
"PQR 30"
"PQR X3"
"PQR X4"
"PQR 10 X3"
"PQR 10 X4"
"PQR 22 X3"
"PQR 22 X4"
"PQR 30 X3"
"PQR 30 X4"

Now I need the unique files list in this folder with certain indexes removed, in this example 10, 22, 30. That means to say my output list should be

['ABC', 'ABC L1', 'ABC L2', 'PQR', 'PQR X3', 'PQR X4' ]

A MWE is given below:

import os
import random
import errno
import itertools
from itertools import repeat
import re

#--------------------------------------
# Create random folders and files

# tzot's forced directory create hack https://stackoverflow.com/a/600612/4576447
def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc:  # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else:
            raise

if not os.path.isdir('./input_folder'):
    os.makedirs('input_folder')
for i in range(4):
    mkdir_p('./input_folder/folder_ABC_' + str(random.randint(100,399)))

for root, dirs, files in os.walk('./input_folder'):
    for dir in dirs:
        for i in repeat(None,4):
            result = open(os.path.join(root,dir) + '/ABC 10 L' + str(random.randint(0,3)) + '.dat','w')
            result = open(os.path.join(root,dir) + '/ABC 22 L' + str(random.randint(0,3)) + '.dat','w')
            result = open(os.path.join(root,dir) + '/ABC 30 L' + str(random.randint(0,3)) + '.dat','w')
            result =  open(os.path.join(root,dir) + '/PQR 10 X' + str(random.randint(0,3)) + '.dat','w')
            result = open(os.path.join(root,dir) + '/PQR 22 X' + str(random.randint(0,3)) + ' .dat','w')
            result = open(os.path.join(root,dir) + '/PQR 30 X' + str(random.randint(0,3)) + '.dat','w')         
            result = open(os.path.join(root,dir) + '/ABC ' + str(random.randint(0,3)) + '.dat','w')
            result = open(os.path.join(root,dir) + '/PQR ' + str(random.randint(0,3)) + '.dat','w')
#--------------------------------------
# Main rename code


remove = [10, 22, 30]

for root, dirs, files in os.walk('./input_folder'):
    for dir in dirs: 
        print (dir)
        output_files = [s for s in os.listdir(os.path.join(root,dir)) if s.endswith('.dat')]

How to find unique files after removing files that have values in a particular list ('remove' in this example)?


Solution

  • This is one approach using re and a list comprehension .

    Ex:

    import re
    
    output_files = ['ABC', 'ABC 10', 'ABC 22', 'ABC 30', 'ABC L1', 'ABC L2', 'ABC 10 L1', 'ABC 10 L2', 'ABC 22 L1', 'ABC 22 L2', 'ABC 30 L1', 'ABC 30 L2', 'PQR', 'PQR 10', 'PQR 22', 'PQR 30', 'PQR X3', 'PQR X4', 'PQR 10 X3', 'PQR 10 X4', 'PQR 22 X3', 'PQR 22 X4', 'PQR 30 X3', 'PQR 30 X4']
    remove = ["10", "22", "30"]
    
    pat = re.compile("(" + "|".join(remove) + ")")
    print( [i for i in output_files if not pat.search(i)])
    

    Output:

    ['ABC', 'ABC L1', 'ABC L2', 'PQR', 'PQR X3', 'PQR X4']