我想概括我的以下代码,以在参数中获取尽可能多的文件,而无需硬编码,例如此处的 f1 和 f2。我怎样才能做到这一点? 这是我的代码。
def wordFreq(f1, f2):
f1 = open(f1, 'r')
f2 = open(f2, 'r')
file_list = [f1, f2]
num_files = len(file_list)
wordFreq = {}
.....
.....
return wordFreq
print(wordFreq('file1.txt','file2.txt'))
最佳答案
你想要*args
def word_freq(*files):
file_list = [open(file, 'r') for file in files]
...
关于python - 如何概括文件函数以打开参数中提供的尽可能多的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53268975/