Search code examples
pythonpython-3.xloopsfile-iolist-comprehension

Reduce the number of list comprehensions and find a better way to open a list of files from a text file


I am trying to read multiple sql files in a repository. I have created the code for it, but it contains multiple list comprehensions. I need to know if there is a better (more pythonic) way of writing this code.

path = '/home/jupyter/SQL_scripts/'
file_list = open("/home/jupyter/a.txt", "r")
d= file_list.read()
file_list.close()

dl = d.split("\n")

def gsql(fpath):
    with open(fpath, 'r') as file:
        return file.read()

dl = [path + x for x in dl if isinstance(x, str)]
ss = [gsql(s) for s in dl]
[mod1.e_sql(x) for x in ss]

Here path is the directory in which the sql files are, and file_list is the text file which has just the file-names of the sql files.

Merging path and file_list gives the whole path to the file, and mod1.e_sql() is the custom module to execute the sql.

The code is required to do this:

  • execute all the sql file names in txt file
  • txt file only contains file names not whole path

Currently I am doing this:

  1. open txt file
  2. read all filenames as list elements
  3. join path + each sql filenames
  4. open that (path + each sql filenames)
  5. read the file content(sql query)
  6. execute that sql query string

I need to eliminate some of the steps, but I am not able to write anything better.


Solution

  • There are a lot of ways to simplify this. Since I don't know if all these intermediate variables have any purpose it's possible that I might be eliminating ones that you need.. but, here's a take on it:

    path = '/home/jupyter/SQL_scripts/'
    
    def gsql(fpath):
        with open(fpath, 'r') as file:
            return file.read()
    
    with open("/home/jupyter/a.txt", "r") as file_list:
        filenames = file_list.read().split("\n")
    
    # Eliminated your check for string type since I don't think it will ever be False.
    for filename in filenames:
        mod1.e_sql(gsql(path + filename))