import os, sys, json total = 0 try: month = sys.argv[1] year = sys.argv[2] except: print ("Wrong syntax. 'python3 monthlypair.py MM YY'") sys.exit() with open(year + month+".csv","w",newline="") as f: for archivo in os.listdir(): if archivo.endswith(".profits"): with open(archivo, "r") as csvfile: for x in csvfile: date,amount,_ = x.split(",") if date.split("-")[1]==month and date.split("-")[0]==year: total += float(amount) #result.append([archivo[:-8],total]) if total != 0: print(archivo[:-8]+","+str(total)) f.write(archivo[:-8]+","+str(total)+"\n") total = 0 # Per-dollar section: with open(year+month+"_per_dollar.csv","w",newline="") as f: for archivo in os.listdir(): if archivo.endswith(".profits"): with open(archivo, "r") as csvfile: for x in csvfile: date,amount,_ = x.split(",") if date.split("-")[1]==month and date.split("-")[0]==year: total += float(amount) #result.append([archivo[:-8],total]) if total != 0: #Here load the order size from the config file file_name = archivo.split(".")[0] with open("/home/nsanc/DCA2_live/configs/"+file_name+".json") as g: read_config = json.load(g) print(archivo[:-8]+","+str(round(total/read_config["order_size"],2))) f.write(archivo[:-8]+","+str(round(total/read_config["order_size"],2))+"\n") total = 0