我试图使用python中的open()函数读取JPG图像的上下文,但是只读取文件的前3行
file = open(dir, 'r') # dir = imgServer.jpg
data = file.read()
print data # only the first 3 lines
我试着一行一行地读:
file = open(dir, 'r') # dir = imgServer.jpg
data = ''
for line in file:
data += line
print data # only the first 3 lines
但结果是一样的
你应该像这样以二进制模式打开文件
默认情况下,它以文本模式打开
对二进制文件使用
'rb'
。另外,我建议您不要使用dir
作为实际的文件名;并为读取操作使用上下文语法相关问题 更多 >
编程相关推荐