I try to open a file (here bytestring is shortened) but get ValueError: embedded null byte
My code:
file = b'\x03\x04\x14\x00'
with open(file) as f:
print(f.name)
I get this:
ValueError: embedded null byte
Here's your code; let's go through it. I see three issues.
file = b'\x03\x04\x14\x00'
with open(file) as f:
print(f.name)
The open(file)
requires a filename or path, not bytes.
Your file
is actually the bytes you'd get from running f.read()
, after opening the file.
Finally, in f.name
, the "name" is probably a "property" of a file Path
(ie from pathlib import Path
).
Typically, the pattern would look more like this:
from pathlib import Path
file = Path("/home/user/docs/spreadsheet.csv")
print(file.name)
# Open file in "read-binary" mode, and read all the content into the "bytes_" variable
with open(file, 'rb') as f:
bytes_ = f.read()
print(bytes_)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.