Compare 2 files csv in Pycharm

Hello everyone

This is my program :

OLD_PATH = ‘Recherche.csv’
NEW_PATH = ‘Travail.csv’

out = open(“Out.txt”, ‘w’)

old = open(OLD_PATH, ‘r’)
old_lines = list(old)
old.close()

new = open(NEW_PATH, ‘r’)
new_lines = list(new)
new.close()

for line in unified_diff(old_lines, new_lines, fromfile=OLD_PATH, tofile=NEW_PATH):
out.write(line)

I would like to compare 2 csv files and display the difference with this program but it displays this error message :

C:\Users\l.rupert\PycharmProjects\Saleforce.venv\Scripts\python.exe C:\Users\l.rupert\PycharmProjects\Saleforce\test.py
Traceback (most recent call last):
File “C:\Users\l.rupert\PycharmProjects\Saleforce\test.py”, line 7, in
old_lines = list(old)
^^^^^^^^^
File “C:\Users\l.rupert\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py”, line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x8d in position 1822: character maps to

Process finished with exit code 1

Can you help me?

Thanks

“old” is just the reference to a module representing an file-opened-for-reading.
You then need to read from it: content = old.read(). Now you can analyze content.

And, please: use proper code formatting! Check Guidelines, if you don’t understand.

When you’re opening the file, you’re not specifying the encoding, so it’s defaulting to cp1252.

Unfortunately, it appears that the contents of the file weren’t written using that encoding.

These days the recommended encoding is UTF-8, so, on the assumption that it might be using that, try explicitly opening the file with that:

old = open(OLD_PATH, ‘r’, encoding='utf-8')