当前位置:网站首页>报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
2022-07-06 09:13:00 【zkkkkkkkkkkkkk】
目录
一、报错代码
with open(pdfpath,"r") as fp:
二、报错信息
PdfReadWarning: PdfFileReader stream/file object is not in binary mode. It may not be read correctly. [pdf.py:1143]
Traceback (most recent call last):
File "D:/Gitlab/my_world/recognize_img/pdf_to_img.py", line 36, in <module>
jiemi('D:\\Gitlab\\my_world\\recognize_img\\demo_img\\无框表格不可编辑.pdf')
File "D:/Gitlab/my_world/recognize_img/pdf_to_img.py", line 9, in jiemi
pdfFile = PyPDF4.pdf.PdfFileReader(fp)
File "C:\Users\dell\Anaconda3\lib\site-packages\PyPDF4\pdf.py", line 1148, in __init__
self.read(stream)
File "C:\Users\dell\Anaconda3\lib\site-packages\PyPDF4\pdf.py", line 1754, in read
stream.seek(-1, 2)
io.UnsupportedOperation: can't do nonzero end-relative seeks
Process finished with exit code 1
二、解决方式
报错时seek()函数引发的异常。解决方法:如果使用open()函数来打开文件而引发的错误时(比如我是用open()函数来打开pdf),必须使用r+b的读取方式。
def read_pdf(file_path):
with open(pdfpath,"rb") as fp:
pdfFile = PyPDF4.pdf.PdfFileReader(fp)
print(pdf.pages)
fp.close()
if __name__ == '__main__':
file_path = 'D:\\Gitlab\\my_world\\recognize_img\\demo_img\\无框表格不可编辑.pdf'
read_pdf(file_path)
四、seek函数介绍
这个报错是由seek()函数引发的异常。seek()函数常见于读取文件中,有指针的作用。一般在读取文档的过程中,指针会随之移动来记录当前已读的位置。seek()函数还可以将指针移动到指定位置。
seek函数的使用
fp.seek(0, 0) # 第一个0代表偏移量,第二个0代表从哪个位置开始偏移(0:从文件开头起,1:从当前位置起,2:从文件末尾起。)
# 打开文件
with open("runoob.txt", "rw+") as fp:
line = fp.readline()
print("读取数据:",line)
# 重新设置文件读取指针到开头
fp.seek(0, 0) # 第一个0代表偏移量,第二个0代表从哪个位置开始偏移(0:从文件开头起,1:从当前位置起,2:从文件末尾起。)
line = fp.readline()
print("重置后读取数据:",line)
# 关闭文件
fp.close()
边栏推荐
- Mysql27 - Optimisation des index et des requêtes
- 【博主推荐】asp.net WebService 后台数据API JSON(附源码)
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- Unicode decodeerror: 'UTF-8' codec can't decode byte 0xd0 in position 0 successfully resolved
- MySQL21-用户与权限管理
- Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
- CSDN问答标签技能树(一) —— 基本框架的构建
- API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
- Discriminant model: a discriminant model creation framework log linear model
- MySQL24-索引的数据结构
猜你喜欢
Mysql24 index data structure
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
MySQL23-存储引擎
CSDN问答模块标题推荐任务(二) —— 效果优化
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
Mysql25 index creation and design principles
Navicat 导出表生成PDM文件
Mysql21 - gestion des utilisateurs et des droits
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
随机推荐
Texttext data enhancement method data argument
JDBC原理
Mysql21 user and permission management
Water and rain condition monitoring reservoir water and rain condition online monitoring
解决:log4j:WARN Please initialize the log4j system properly.
Principes JDBC
Postman environment variable settings
[untitled]
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
Ansible practical Series III_ Task common commands
Global and Chinese market of operational amplifier 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql21 - gestion des utilisateurs et des droits
Installation and use of MySQL under MySQL 19 Linux
API learning of OpenGL (2002) smooth flat of glsl
Mysql27 index optimization and query optimization
@Controller, @service, @repository, @component differences
Mysql30 transaction Basics
CSDN问答模块标题推荐任务(二) —— 效果优化
February 13, 2022-2-climbing stairs
[leectode 2022.2.13] maximum number of "balloons"