简体   繁体   中英

How to open a file in ternary in python?

I am an amateur python coder. I was wondering if there was a way to open a file in ternary? It would really help to know soon. Something like this but ternary:

with open("C:/Users/jfamily/Documents/NJ_Science_Fair/binary_open_test.txt", 
"rb") as binaryfile :
data = bytearray(binaryfile.read())
print(data)

From the docs about open

https://docs.python.org/3/library/functions.html#open

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

So really "binary" here means bytes. Those bytes could contain data in some kind of ternary encoding, but you'd likely still open the file as binary and decode them to your ternary spec yourself.

There is no such facility in Python. The language supports binary because it's a common file format; ternary is not.

Of course, if you find it useful, I suggest that you write a ternary package of your own.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM