我正在研究一个函数,它从一个文件中逐个读取多个文件。我设置了在文件中获取大量文件名的方法,但我不确定在循环序列中应该使用什么。这里是我现在在哪里:如何逐个打开多个文件并使用Python打印内容
with open('userdata/addedfiles', 'r') as read_files:
number_of_files = sum(1 for _ in read_files)
print(number_of_files, "files added")
print("File contents: \n")
# get individual filenames in file and
# read them one by one
for x in range(number_of_files):
# hmm...
有什么建议在这里使用?谢谢。
'NUMBER_OF_FILES = SUM(1 _在read_files )'等于'number_of_files = len(read_files)'....对吗? –
@JoseRicardoBustosM。 ''_io.TextIOWrapper'没有len()' –
你正在看到相反的问题。 1)找到一个简单的方法来遍历所有的文件名和2)读取它的内容并打印它。 http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python – Joe