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:
Currently I am doing this:
I need to eliminate some of the steps, but I am not able to write anything better.
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))