在Python中读取字节流
我正在使用 Python appscript 将插图写入我的 iTunes 歌曲。我有一个以 .pict 格式存储的文件,当我使用正常的 open
和 read
例程时,它会将内容读取为字符串(以 utf-8 编码)。
imFile = open('/Users/kartikaiyer/temp.pict','r')
data = imFile.read()
it = app('iTunes')
sel = it.current_track.get()
sel.artworks[1].data_.set(data[513:])
是我正在使用的代码。它因无法识别的对象而失败,我猜是因为 set 参数是 utf-8 编码的字符串,关于如何将 data
强制为字节流并将其用作集合的任何想法范围。 BinAscii模块没有我需要的功能。任何帮助将不胜感激。
I'm using Python appscript to write artwork to my iTunes Songs. I have a file stored in .pict format and when I use the normal open
and read
routines, it reads the content as a string (encoded in utf-8).
imFile = open('/Users/kartikaiyer/temp.pict','r')
data = imFile.read()
it = app('iTunes')
sel = it.current_track.get()
sel.artworks[1].data_.set(data[513:])
Is the code I'm using. it fails with a objct not recognized and I'm guessing its because the set parameter is a utf-8 encoded string, Any ideas as to how I can coerce data
to a bytestream and use that as a set parameter. BinAscii module doesn't have the functions I need. Any help would be much appreciated.
尝试将读取模式设置为二进制:
Try setting the read mode to binary: