I am trying to create List comprehension for a list inside this function. I wonder if it is possible to make it cleaner.
def load_list():
review_list = []
for counter, entry in enumerate(os.listdir('data/review')):
if os.path.isfile(os.path.join('data/review', entry)):
with open(f'data/review/{counter}.txt', encoding='utf-8') as fp:
review_list.append(fp.read().splitlines())
return review_list
Assumption
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:
Then we can use the Walrus operator to both simplify original code and make a list comprehension
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.