Search code examples
pythonfileis-empty

Data does not print into the file, resulting in a empty file


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.


Solution

  • 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.