Active Oldest Votes. To read one byte: file. Improve this answer. Mark Byers Mark Byers k gold badges silver badges bronze badges. I thought that file. You can use file. Scott Griffiths Scott Griffiths The code you've shown will read 8 bytes. Owen Champion Owen Champion 21 2 2 bronze badges. If you're just using it for a single character, surely you'd do better to just use ord f.
Late to the party, but this may help anyone looking for a quick solution: you can use bin ord 'b'. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Create bytearray. Bytearray can be cast to bytes. Attention geek!
Strengthen your foundations with the Python Programming Foundation Course and learn the basics. Next Python - Reverse a words in a line and keep the special characters untouched. Recommended Articles. Find length of one array element in bytes and total bytes consumed by the elements in Numpy. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. They both have to waste time reading through the whole file. A more efficient way would be to jump to the end of the file and read it backward to find a newline.
In order to seek the end of file, we have to open the file in binary mode. We seek to the last 2 characters since the file is likely to end with a newline. Then, keep reading a single byte until a newline character is found. And, finally, we use readline to read the final line, and decode to convert from bytes to string.
Xuan Ngo is the founder of OpenWritings. He currently lives in Montreal, Canada.
0コメント