当前位置:网站首页>报错解决 —— 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()
边栏推荐
- C语言标准的发展
- MySQL21-用户与权限管理
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- 【博主推荐】C# Winform定时发送邮箱(附源码)
- Mysql21 user and permission management
- API learning of OpenGL (2001) gltexgen
- Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
- Yum prompt another app is currently holding the yum lock; waiting for it to exit...
- @Controller, @service, @repository, @component differences
猜你喜欢
CSDN question and answer tag skill tree (I) -- Construction of basic framework
CSDN-NLP:基于技能树和弱监督学习的博文难度等级分类 (一)
【博主推荐】C# Winform定时发送邮箱(附源码)
Valentine's Day is coming, are you still worried about eating dog food? Teach you to make a confession wall hand in hand. Express your love to the person you want
MySQL 29 other database tuning strategies
Navicat 導出錶生成PDM文件
csdn-Markdown编辑器
CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Idea import / export settings file
随机推荐
[reading notes] rewards efficient and privacy preserving federated deep learning
CSDN问答模块标题推荐任务(二) —— 效果优化
LeetCode #461 汉明距离
Yum prompt another app is currently holding the yum lock; waiting for it to exit...
Moteur de stockage mysql23
MySQL 29 other database tuning strategies
Copy constructor template and copy assignment operator template
MySQL24-索引的数据结构
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Pytorch RNN actual combat case_ MNIST handwriting font recognition
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Ansible实战系列三 _ task常用命令
[paper reading notes] - cryptographic analysis of short RSA secret exponents
February 13, 2022-2-climbing stairs
MySQL21-用戶與權限管理
CSDN question and answer module Title Recommendation task (II) -- effect optimization
MySQL25-索引的创建与设计原则
Baidu Encyclopedia data crawling and content classification and recognition
Asp access Shaoxing tourism graduation design website
Postman Interface Association