i have a code where i can successfully execute a sql file and export the results to a excel file:
#CONFIG
DATE = datetime.datetime.now().strftime("%Y-%m-%d_%I-%M-%p")
CONFIG = f'./config'
DATA = f'./data'
TODAY = f'{DATA}/{DATE}'
#Server Config
SQLSERVER = cx_Oracle.makedsn('xxx.xxx.xxx.xxx', '1521', service_name='SERVER')
conn = cx_Oracle.connect(user='xxxxx', password='xxxxxx', dsn=SQLSERVER)
# Read SQL File
fd = open(f'{CONFIG}/1.sql', 'r')
Query1 = fd.read()
fd.close()
#Execute SQL query
dfs = []
for chunk in pd.read_sql_query(Query1, con=conn):
dfs.append(chunk)
df = pd.concat(dfs)
#Write results to Excel file
df.to_excel(f'{TODAY}/Query1.xlsx',engine='xlsxwriter',index=False)
This is all working greate, as expected. But the problem is that i cannot make it in a loop, because in the config folder i have around 60 sql files. And every week i got more and more. How to put all this in a loop to automaticly read all of the sql files and write a separate excel for each sql query?