Я посмотрел другие посты, касающиеся этой TypeError, но они не помогли мне понять это. Там, где происходит ошибка, я пытаюсь просмотреть список возвращенных файлов из функции геотекстиля, а затем найти их для ввода пользователя. Но похоже, что он не может войти в цикл for for in in: из-за NoneType. Что является причиной того, что список файлов не имеет типа?
# Program to accept user input and search all .txt files for said input
import re, sys, pprint, os
def getTxtFiles():
# Create a list of all the .txt files to be searched
files = []
for i in os.listdir(os.path.expanduser('~/Documents')):
if i.endswith('.txt'):
files.append(i)
def searchFiles(files):
''' Asks the user for input, searchs the txt files passed,
stores the results into a list'''
results = []
searchForRegex = re.compile(input('What would you like to search all the text files for?'))
for i in files:
with open(i) as text:
found = searchForRegex.findall(text)
results.append(found)
txtFiles = getTxtFiles()
print(searchFiles(txtFiles))
Traceback (most recent call last):
File "searchAll.py", line 26, in <module>
print(searchFiles(txtFiles))
File "searchAll.py", line 19, in searchFiles
for i in files:
TypeError: 'NoneType' object is not iterable