1

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
Paszymaja
  • 13
  • 3
  • 2
    "with" is not allowed in a list comprehension. I also doubt that the code would become cleaner if it is stuffed in a comprehension. – Michael Butscher Jun 15 '20 at 11:10
  • Is there a reason you're getting file entries 1) `os.listdir('data/review')`, 2) checking the existence of file entry `os.path.isfile(os.path.join('data/review', entry)`, 3) but opening based upon loop iteration index: `open(f'data/review/{counter}.txt', encoding='utf-8')`? – DarrylG Jun 15 '20 at 11:43

1 Answers1

0

Assumption

  1. 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:
  1. 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

  1. 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.

DarrylG
  • 16,732
  • 2
  • 17
  • 23