Os-path Join两个参数



我需要帮助按名称查找文件夹中的文件,我可以用一个文件名来查找,如何用两个文件名?这是使用的代码

path = r"Z:/Equities/ReferencePrice/"
files = []
for file in glob.glob(os.path.join(path ,"*OptionOnEquitiesReferencePriceFile*"+"*.txt*")):
df = pd.read_csv(file, delimiter = ';')

第一个文件包含名称

"OptionOnEquities ReferencePriceFile;

第二个文件包含名称

"BDR参考价格";

如何放置第二个文件如何在一个或另一个或两个之间搜索

我认为你不能用简单的方式做到这一点,所以这里有一个你可以使用的替代解决方案(带函数(:

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))

输出:

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

最新更新