Search code examples
pythonlist-comprehension

List comprehension with 'with open()' inside


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

Solution

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