I cant read a file, and I dont understand why:
f = open("test/test.pdf", "r")
data = list(f.read())
print data
Returns : []
I would like to open a PDF, and extract every bytes, and put it in a List.
What's wrong with my code ? :(
Thanks,
The open() function opens a file in text format by default. To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing.
To read from a binary file, we need to open it with the mode rb instead of the default mode of rt : >>> with open("exercises. zip", mode="rb") as zip_file: ... contents = zip_file. read() ...
f = open("test/test.pdf", "rb")
You must include the pseudo-mode "b" for binary when reading and writing on Windows. Otherwise the OS silently translates what it considers to be "line endings", causing i/o corruption.
Jonathan is correct that you should be opening the file in binary mode if you are on windows.
However, a PDF file will start with "%PDF-", which would at least be read in regardless of whether you are using binary mode or not.
So it appears to me that your "test/test.pdf" is an empty file
b
in the mode of your file, i.e. open(filename, "rb")
.
b
doesn't hurt anything, though it does not mean anything.f = open("test/test.pdf", "rb")
, say with open("test/test.pdf", "r") as f:
. This will assure your file always gets closed.list(f.read())
is not likely to be useful code very often. f.read()
reaurns a str
and calling list
on it makes a list of the characters (one-byte strings). This is very seldom needed.read
should work. Are you positive that there is anything in test/test.pdf
? Python does not seem to think there is.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With