I've been trying to have this data write to the "scenario_out.txt" file. However, when I run the code I do not any get errors but my text file does come out to empty, so no data was ever put into the file. . I'm reviewing my notes but I don't see where I went wrong.
# Display results of scenario
def file_save_flow(save_to_file, # save to a file object if save_to_file = True
file_object, # name of file object to save to
scen_year,
age,
s_bal,
bal_pv,
inv_gains,
ann_savings,
ss_payment,
major_inc,
major_exp,
tax_rate,
tar_ret_spend,
net_ret_spend,
ret_pre_tax_spend,
taxes,
end_bal
):
""" file_save_flow() test output
>>> out_file = open("scenario_out.txt","w")
>>> file_save_flow(save_to_file = True,
... file_object = out_file,
... scen_year = 0,
... age = 60,
... s_bal = 100000,
... bal_pv = 100000,
... inv_gains = 1000,
... ann_savings = 1000,
... ss_payment = 1000,
... major_inc = 1000,
... major_exp = 1000,
... tax_rate = 0.15,
... tar_ret_spend = 50000,
... net_ret_spend = 40000,
... ret_pre_tax_spend = 60000,
... taxes = 10000,
... end_bal = 120000
... )
>>> out_file.close()
>>> in_file = open("scenario_out.txt","r")
>>> print(in_file.read())
Year :0
Age :60
Start Bal :100,000.00
Start Bal PV :100,000.00
Invest Gains :1,000.00
Ann Savings :1,000.00
SS Payment :1,000.00
Major Inc :1,000.00
Major Exp :1,000.00
Tax Rate :15.00%
Targ Ret Spend :50,000.00
Net Ret Spend :40,000.00
Ret Pre-tax Spend:60,000.00
Taxes :10,000.00
End Bal :120,000.00
<BLANKLINE>
>>> in_file.close()
"""
#=============================My Code================================
if save_to_file == True:
print(' Year :' , scen_year,
'\n Age :',age,
'\n Start Bal :', (format(s_bal, ',.2f')) ,
'\n Start Bal PV :', (format(bal_pv, ',.2f')),
'\n Invest Gains :', (format(inv_gains, ',.2f')),
'\n Ann Savings :', (format(ann_savings,',.2f')),
'\n SS Payment :', (format(ss_payment, ',.2f')),
'\n Major Inc :', (format(major_inc, ',.2f')),
'\n Major Exp :', (format(major_exp, ',.2f')),
'\n Tax Rate :', (format(tax_rate, '.2%')),
'\n Targ Ret Spend :', (format(tar_ret_spend, ',.2f')),
'\n Net Ret Spend :', (format(net_ret_spend, ',.2f')),
'\n Ret Pre-tax Spend :', (format(ret_pre_tax_spend, ',.2f')),
'\n Taxes :', (format(taxes, ',.2f')),
'\n End Bal :', (format(end_bal, ',.2f')))
#============================================================================
# test function
out_file = open("scenario_out.txt","w") # set up the file object to write to
file_save_flow(save_to_file = True,
file_object = out_file,
scen_year = 0,
age = 60,
s_bal = 100000,
bal_pv = 100000,
inv_gains = 1000,
ann_savings = 1000,
ss_payment = 1000,
major_inc = 1000,
major_exp = 1000,
tax_rate = 0.15,
tar_ret_spend = 50000,
net_ret_spend = 40000,
ret_pre_tax_spend = 60000,
taxes = 10000,
end_bal = 120000
)
out_file.close()
in_file = open("scenario_out.txt","r") # read back the contents saved to the file
print(in_file.read())
in_file.close()
I've tied some bool variable to use as a flag , but still no luck.
CodePudding user response:
At the moment, you're just printing your output to the console, rather than actually saving it to the file.
You probably want something like
if save_to_file == True:
output = (
' Year : ' str(scen_year)
'\n Age : ' str(age)
'\n Start Bal : ' (format(s_bal, ',.2f'))
'\n Start Bal PV : ' (format(bal_pv, ',.2f'))
'\n Invest Gains : ' (format(inv_gains, ',.2f'))
'\n Ann Savings : ' (format(ann_savings,',.2f'))
'\n SS Payment : ' (format(ss_payment, ',.2f'))
'\n Major Inc : ' (format(major_inc, ',.2f'))
'\n Major Exp : ' (format(major_exp, ',.2f'))
'\n Tax Rate : ' (format(tax_rate, '.2%'))
'\n Targ Ret Spend : ' (format(tar_ret_spend, ',.2f'))
'\n Net Ret Spend : ' (format(net_ret_spend, ',.2f'))
'\n Ret Pre-tax Spend : ' (format(ret_pre_tax_spend, ',.2f'))
'\n Taxes : ' (format(taxes, ',.2f'))
'\n End Bal : ' (format(end_bal, ',.2f'))
)
file_object.write(output)
This creates a big string of your output data, then writes it to your file object.
CodePudding user response:
If you remove all the fluff, your code looks exactly like this:
def file_save_flow(save_to_file, file_object, data):
if save_to_file == True:
print('Data: ', data)
out_file = open("scenario_out.txt", "w")
file_save_flow(save_to_file=True, file_object=out_file, data='data')
out_file.close()
in_file = open("scenario_out.txt", "r")
print(in_file.read())
in_file.close()
From that, it's immediately clear that your function does only one thing: it prints something if save_to_file
is True
and it prints nothing otherwise. It doesn't do anything with file_object
.
So, although you create and open a file and pass the file handle to your function, you don't actually use it in the print()
statement.
This would work:
def file_save_flow(save_to_file, file_object, data):
if save_to_file == True:
print('Data: ', data, file=file_object)
else:
print('Data: ', data)
Or, to avoiding repeating yourself:
def file_save_flow(save_to_file, file_object, data):
print('Data: ', data, file=file_object if save_to_file else None)