[Python] Ejemplo escritura en un fichero
def main(): principal = 1000 # Initial amount rate = 0.05 # Interest rate numyears = 5 # Number of years year = 1 f = open("C:\\Users\\oscar\\Documents\\out.txt","w") # Open file for writing while year <= numyears: principal = principal * (1 + rate) #print >>f,"%3d %0.2f" % (year,principal) ####python2 print("%3d %0.2f" % (year,principal),file=f) #python3 #otra forma: #f.write("%3d %0.2f\n" % (year,principal)) year += 1 f.close() if __name__ == '__main__': main()
Comentarios