"ValueError: embedded null character" when using open ()

I take python in my college and I am stuck in my current assignment. We need to take 2 files and compare them. I'm just trying to open files so that I can use them, but I keep getting the error "ValueError: embedded null character"

 file1 = input("Enter the name of the first file: ") file1_open = open(file1) file1_content = file1_open.read() 

What does this error mean?

+5
source share
1 answer

The default encoding for Python 3.5 files is "utf-8."

By default, file encoding for Windows tends to be something else.

If you are going to open two text files, you can try the following:

 import locale locale.getdefaultlocale() file1 = input("Enter the name of the first file: ") file1_open = open(file1, encoding=locale.getdefaultlocale()[1]) file1_content = file1_open.read() 

Auto-discovery should be detected in the standard library.

Otherwise, you can create your own:

 def guess_encoding(csv_file): """guess the encoding of the given file""" import io import locale with io.open(csv_file, "rb") as f: data = f.read(5) if data.startswith(b"\xEF\xBB\xBF"): # UTF-8 with a "BOM" return "utf-8-sig" elif data.startswith(b"\xFF\xFE") or data.startswith(b"\xFE\xFF"): return "utf-16" else: # in Windows, guessing utf-8 doesn't work, so we have to try try: with io.open(csv_file, encoding="utf-8") as f: preview = f.read(222222) return "utf-8" except: return locale.getdefaultlocale()[1] 

and then

 file1 = input("Enter the name of the first file: ") file1_open = open(file1, encoding=guess_encoding(file1)) file1_content = file1_open.read() 
+3
source

Source: https://habr.com/ru/post/1237024/


All Articles