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:
- open txt file
- read all filenames as list elements
- join path + each sql filenames
- open that (path + each sql filenames)
- read the file content(sql query)
- execute that sql query string
I need to eliminate some of the steps, but I am not able to write anything better.