Answer the question
In order to leave comments, you need to log in
Why doesn't filter work?
There is a script that looks for a file in a folder, I take the name of the file from the namefiles.txt file,
so if you take a string from the file, then fnmatch.filter will not find anything,
if you manually enter pattern = 'test_GK1*' into the pattern variable, then the file will be found!
(an asterisk is needed because there may be other characters in the file name)
Tell me why, when reading from a file into a variable, filter does not see the file?
contents of namefiles.txt
GK0
GK1
GK2
import fnmatch
import os
path = 'C:\temp'
typefiles = []
listfile = codecs.open('namefiles.txt', "r", "utf-8")
for line in listfile:
typefiles.append(line.replace('\n', ''))
listfile.close()
for file in typefiles:
pattern = f'тест_{file}*'
matches = []
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
print(matches)
Answer the question
In order to leave comments, you need to log in
solved the problem, the text file had to be opened with utf-8-sig
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question