python - 如何概括文件函数以打开参数中提供的尽可能多的文本文件?

标签 python python-3.x file dictionary word-frequency

我想概括我的以下代码,以在参数中获取尽可能多的文件,而无需硬编码,例如此处的 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/

相关文章:

python - 将字符串格式映射到数字列表

c - 将文件写入结构体数组

python - 创建新数据库时如何将参数传递给execute()

python - tensorflow尝试在类中使用未初始化的值W

Python:附加/合并多个尊重标题的 csv 文件并写入 csv

python - 从 for 循环返回的元素中追加一个新列表

Python3 Flask 在服务器内存中上传文件

python-3.x - 使用 os.system 函数时设置超时

jQuery - 单击按钮时,模拟输入[类型 ="file"]。仅在父元素中更改