save data from a file in another Python file

0

I'm in doubt about how to save a sum that contains numbers in one file and pass it to another file (data.txt) I leave the example if I want to add it is line2, the problem is that I do not create the second file.

#Primero creo el fichero
def creamos(nombre):
 fichero=open(nombre,"w")
 fichero.close()
#Escribo Texto y numeros para hacer su suma 
def escribimos(nombre):
 fichero=open(nombre,"w")
 linea="Hola stackoverflow 2 3 \n"
 fichero.write(linea)
 linea2="necesito su ayuda 2 4\n"
 fichero.write(linea2)
 fichero.close()

def guardarenOtroFichero(nombre):
 fichero=open(nombre,"r") #Leo el fichero
 fichero2=open(nombre,"w") #Creo otro para guardar la suma calculada
 suma=0
 linea=fichero.readline()
 linea=fichero.readline() #Aqui leo la segunda linea 
 lista= linea.split() #separo todos los elementos
 for x in lista:
    try:
        if isinstance(int(x), (int)): #Valido si hay enteros
            suma = suma +  int(x)

    except Exception:
        print(x,"Es una palabra")

print("la suma de los numeros ",suma)

Main Class.

creamos("datos.txt")
escribimos("datos.txt")
guardarenOtroFichero("suma.txt)
    
asked by Rebc3sp 05.06.2018 в 12:01
source

0 answers