我想在gensim中只为它的扩展名加载一个文件。
正常的代码是这样的:
model = gensim.models.word2vec.Word2Vec.load("news.bin")
但是我想用“.bin”自动打开任何文件。
例:
model = gensim.models.word2vec.Word2Vec.load(***I would like to change this part to only load any .bin***)
.bin文件:
它可以是“news.bin”,“file.bin”或“guess.bin”。只要它只加载扩展名。谢谢。
如果要逐个打开所有这些,可以迭代目标目录中的文件。
这是Python 3的代码示例:
import os
directory_path = "/path/to/directory"
for filename in os.listdir(directory_path):
if filename.endswith(".bin"):
file_path = os.path.join(directory_path, filename)
model = gensim.models.word2vec.Word2Vec.load(file_path)
# Do whatever you want to do with model
如果你只想打开其中任何一个,你可以在第一场比赛后突破for循环:
import os
directory_path = "/path/to/directory"
for filename in os.listdir(directory_path):
if filename.endswith(".bin"):
file_path = os.path.join(directory_path, filename)
model = gensim.models.word2vec.Word2Vec.load(file_path)
# Do whatever you want to do with model
# Break out of the for loop afterwards so it stops iterating
break
glob
库是完美的:
from glob import glob
for file_name in glob('*.bin'):
model = gensim.models.word2vec.Word2Vec.load(file_name)
这将根据Unix样式路径名模式扩展循环遍历与*.bin
匹配的所有文件。