I have many py files need to run in one py file. But I would like to make some condition which is need to exit the program when it occur an error during the process in any py file. Anyone can help me to solve it? Thanks.
5250A have error --> exit program
5250A no error --> 5250B have error --> exit program
5250A no error --> 5250B no error --> 5250C have error --> exit program
and so on.....
UPDATED: 2021-03-12
main.py:
import _5250A_1
import _5250A_2
import _5250A_3
import _5250A
def main():
try:
_5250A_1.main()
sleep(3)
_5250A_2.main()
sleep(3)
_5250A_3.main()
sleep(3)
_5250.main()
except Exception as details:
print(f"Controller failed with error {details}")
raise
finally:
print("Controller completed")
if __name__ == "__main__":
main()
_5250A
import os
import glob
import pandas as pd
import openpyxl as xl;
import datetime
#Define some variable
activeUser = os.getlogin()
currDateTime = datetime.datetime.today() - datetime.timedelta(days = 31)
prevDateTime = datetime.datetime.today() - datetime.timedelta(days = 62)
#Merge 3 csv files into 1 csv file
os.chdir("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A")
allFiles = [i for i in glob.glob("*.{}".format("csv"))]
mergeCsv = pd.concat([pd.read_csv(f) for f in allFiles ])
#Export to csv file
mergeCsv.to_csv("5250A.csv", header = 1, index = False)
#Run Pandas and openpyxl to load csv file and convert to xlsx file format
csvFile = pd.read_csv ("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.csv")
csvFile.to_excel ("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx", sheet_name="5250A (Curr)", header = True, index = None)
wb1 = xl.load_workbook("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + prevDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx")
ws1 = wb1.worksheets[0]
wb2 = xl.load_workbook("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx")
ws2 = wb2.create_sheet("5250A (Prev)")
mr = ws1.max_row
mc = ws1.max_column
for i in range (1, mr + 1):
for j in range (1, mc + 1):
ws2.cell(row = i, column = j).value = ws1.cell(row = i, column = j).value
wb2.save("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx")
#Run openpyxl to subtract cell value between 2 worksheet and save to xlsx file
def get_row_values(worksheet):
result = []
for i in worksheet.rows:
row_data = []
for j in i:
row_data.append(j.value)
result.append(row_data)
return result
if __name__ == "__main__":
wb = xl.load_workbook("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx")
ws1 = wb.worksheets[0]
ws2 = wb.worksheets[1]
ws1_rows = get_row_values(ws1)
ws2_rows = get_row_values(ws2)
ws_new = wb.create_sheet('5250A (New)')
ws_new.append(ws1_rows[0])
for row in range(1, len(ws1_rows)):
row_data = []
for column, value in enumerate(ws1_rows[row]):
if column == 0:
row_data.append(value)
else:
if ws1_rows[row][0] == ws2_rows[row][0]:
row_data.append(value - ws2_rows[row][column])
ws_new.append(row_data)
wb.save("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.xlsx")
#Remove all csv files
os.remove("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.1.csv")
os.remove("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.2.csv")
os.remove("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.3.csv")
os.remove("C:\\Users\\" + activeUser + "\\Documents\\" + "Print Count (" + currDateTime.strftime ("%b %Y") + ")\\5250A" + "\\5250A.csv")