Os path Join Two args

56 Views Asked by At

i need help to find a file inside a folder by name, i can do this with one file name, how could i do this with two file name? This is the code used

path = r"Z:/Equities/ReferencePrice/"
files = []

for file in glob.glob(os.path.join(path ,"*OptionOnEquitiesReferencePriceFile*"+"*.txt*")):
    df = pd.read_csv(file, delimiter = ';')

the first file contains the name

"OptionOnEquitiesReferencePriceFile"

the Second file contains the name

"BDRReferencePrice"

how to place the second file how to search between one or the other or both

1

There are 1 best solutions below

0
mrCopiCat On

I dont think you can do that in a straightforward way, so here's an alternative solution (with a function) that you can use :

import os
from fnmatch import fnmatch

# folder path :
# here in this path i have many files some start with 'other'
# some with 'test and some with random names. 
# in the example im fetchinf only the 'test' and 'other' patterns
dir_path = './test_dir'

def find_by_patterns(patterns, path):
    results = []
    # check for any matches and save them in the results list
    for root, dirs, files in os.walk(path):
        for name in files:
            if max([fnmatch(name, pattern) for pattern in patterns]):
                results.append(os.path.join(root, name))
    return results

# printing the results
print(find_by_patterns(['test*.txt', 'other*.txt'], dir_path))

output:

['./test_dir/other1.txt', './test_dir/other2.txt', './test_dir/test1.txt', './test_dir/test2.txt', './test_dir/test3.txt']