python - IO 错误 [Errno 2]

标签 python pdf random printing pypdf

所以我是一名初级程序员,python 是我的第一语言。我正在尝试编写一个脚本,该脚本将从目录中打开一个随机 PDF,并从该 PDF 中选择一个随机页面进行阅读。当我运行我的脚本时,我得到错误代码 IO ERROR: [Errno 2] 然后显示所选 PDF 的标题。我怎样才能解决这个问题?我正在使用 pyPdf 模块。您可以看到代码中还有其他问题吗?

    import os, random, pyPdf

    from pyPdf import PdfFileReader

    b = random.choice(os.listdir("/home/illtic/PDF"))

    pdf_toread = pyPdf.PdfFileReader(open(b, 'r'))

    last_page = pdf_toread.getNumPages() - 1

    page_one = pdf_toread.getPage(random.randint(0, last_page))

    print " %d " % page_one

最佳答案

b 有什么值(value)?我很确定它只是没有路径的文件名。尝试在文件名前添加路径,应该没问题。

pdf_toread = pyPdf.PdfFileReader(open('/home/illtic/PDF/' + b, 'r'))

关于python - IO 错误 [Errno 2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018840/

相关文章:

Python:将空列表作为值添加到字典

Python-pptx: “Show legend overlapping the chart”

VBA:在 Excel 中如何将 Word 文档另存为 PDF?

c++ - 如何在C/C++中生成0到用户定义整数之间的随机数?

java - 生成一次出现一个数字的随机数

python - 在定义一个python类时,如何在其中设置一个随机变量?

python - 将 Pandas 数据帧中的所有 NaT 值更改为 Timedelta 00 :00:00

xcode - DiskImageCache : Could not resolve the absolute path of the old directory. 未能找到 PDF header : `%PDF' 未找到

python - 使用 tempfile 在 Flask 中创建 pdf/xls 文档

python - 不修改 Django wsgi.conf 的 AWS ElasticBeanstalk 更新