-1

I would like to ask how do I open two files with data inside and subtract them then store them into a file ?

     with open("bignum.txt","r") as f:
                score=f.read()
                score_ints =[int(x) for x in score.split()]

        with open("smallnum.txt","r")as d:
            score_y = d.read()
            score_y_ints =[int(x) for x in score_y.split()]
        difference = [x - y for x, y in zip(score_ints, score_y_ints) ]

smallnum.txt
10000 120

bignum.txt 99 2220

Expected outcome : 9901 -2100

so this is my previous code . It will output it as list but this isn't what I wanted.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
jhan
  • 3
  • 2

1 Answers1

0
with open("f1.txt", "r") as f:
    f1 = f.read()
    f1_ints = [int(x) for x in f1.split()]


with open("f2.txt", "r") as f:
    f2 = f.read()
    f2_ints = [int(x) for x in f2.split()]
    
with open("f1_minus_f2.txt", 'w') as f:
    for i in range( min( len(f1_ints), len(f2_ints)) ) : 
        f.write(str(f1_ints[i] - f2_ints[i]))
        f.write(' ')
SergeS
  • 11
  • 2