0

I am trying to read from 2 text files (001.txt and temp.txt) and compare the numbers. I am opening the files, reading from them, and using int() to convert the strings to ints. The files only contain a single number, 0 for both in this case. I am getting the error ValueErrorr: invalid literal for int() with base 10: '\ufeff0'. I have checked the text files and there are no spaces or extra lines. I am not sure what the issue is. Any advice would be much appreciated!

code:

localfile = open("001.txt", "r") 
local_num = localfile.read()
tempfile_name = "temp.txt"
tempfile = open(tempfile_name, "r")
if(int(local_num) == int(temp_num)):
    print("same")
elif(int(local_num) != int(temp_num)):
    print("different")
tempfile.close()
localfile.close()
Aashish Chaubey
  • 589
  • 1
  • 8
  • 22
Swollos
  • 1
  • 1

1 Answers1

1

May be as simple as this:

with open('file1.txt', 'r') as file1, open('file2.txt', 'r') as file2:
    file1_num = int(file1.readline().strip())
    file2_num = int(file2.readline().strip())

if(int(file1_num) == int(file2_num)):
    print("same")
else:
    print("different")

Actually you can open multiple files in the same line, this would make the code look a little cleaner! Hope that helps!

Aashish Chaubey
  • 589
  • 1
  • 8
  • 22