如何概括文件功能以打开参数中提供的尽可能多的文本文件?
How to generalize the file function to open as many text files as provided in parameter?
我想概括我的以下代码以在不进行硬编码的情况下在参数中获取尽可能多的文件,例如此处的 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]
...
我想概括我的以下代码以在不进行硬编码的情况下在参数中获取尽可能多的文件,例如此处的 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]
...