Assumption
- There's an error in the post.
This line using counter:
with open(f'data/review/{counter}.txt', encoding='utf-8') as fp:
Should be using entry:
with open(f'data/review/{entry}.txt', encoding='utf-8') as fp:
- We're limiting solution to Python 3.8+
Then we can use the Walrus operator to both simplify original code and make a list comprehension
- If files are not explicitly closed and are not under a Context manager they will be Automatically Closed when its Reference Count Hits Zero
Walrus Operator in Original Code
import os
def load_list():
review_list = []
for entry in os.listdir('data/review'):
if os.path.isfile(file_path := os.path.join('data/review', entry)):
with open(file_path, encoding='utf-8') as fp:
review_list.append(fp.read().splitlines())
return review_list
Rewriting as List Comprehension
from os import listdir
from os.path import isfile, join
def load_list():
return [open(file_path, encoding='utf-8').read().splitlines() for entry in listdir('data/review') if isfile(file_path := join('data/review', entry))]
Relying upon assumption 3 to close files in list comprehension.