In a directory, there are two files that share most of their names:
my_file_0_1.txt
my_file_word_0_1.txt
I would like to open my_file_0_1.txt
I need to avoid specifying the exact filename, and instead need to search the directory for a filename that matches the partial string my_file_0
.
From this answer here, and this one, I tried the following:
import numpy as np
import os, fnmatch, glob
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
if __name__=='__main__':
#filename=find('my_file_0*.txt', '/path/to/file')
#print filename
print glob.glob('my_file_0' + '*' + '.txt')
Neither of these would print the actual filename, for me to read in later using np.loadtxt
.
How can I find and store the name of a file, based on the result of a string match?