使用Python的open()函数读取JPG文件时,只读取部分文件

2024-09-27 23:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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

但结果是一样的


Tags: the函数图像onlydatadirlineopen
2条回答

你应该像这样以二进制模式打开文件

file = open(dir, 'rb')

默认情况下,它以文本模式打开

对二进制文件使用'rb'。另外,我建议您不要使用dir作为实际的文件名;并为读取操作使用上下文语法

with file = open(filename, 'rb')
     data = file.read()

相关问题 更多 >

    热门问题