【问题标题】:file() in python 2 cannot be replaced with open() in python 3 for pdfminerpython 2中的file()不能被python 3中的open()替换为pdfminer
【发布时间】:2023-04-03 23:56:01
【问题描述】:

我正在尝试在路径中获取我所有 pdf 的 xml 文件,为此我想在 python 3 上使用来自 https://github.com/euske/pdfminer/blob/master/tools/pdf2txt.py 的 pdfminer 代码。我还安装了 pdfminer.six 和所有相关软件包。但是,使用 file('', 'rb') 打开文件存在一个问题,应将其替换为 open ('', 'rb')。但是我从 open() 得到的输出与 file() 不同,因此写在 github 链接上的函数没有运行我的 pdf 文件。

例如,

在python 2中,返回以下代码;

fp = file(filepath, 'rb') 

<open file '...\\lehsfil2.pdf', mode 'rb' at 0x04ADB180>

而python 3中file()函数的对应关系返回;

fp = open(filepath, 'rb') 

<_io.BufferedReader name='...\\lehsfil2.pdf'>

python2中的file()和python3有直接对应关系,返回的是同一个对象吗?

【问题讨论】:

  • 在 python2 中,open 以与file 相同的方式返回文件(有关详细信息,请参阅stackoverflow.com/questions/32131230/python-file-function)。在 python3 中 file 已被删除,但 open 继续像在 python2 中一样工作,API 略有相同。当您尝试将代码中的file 替换为open 时,您遇到了什么错误?我看了一下库,所有对 api 的使用都应该出现在 python2 和 python3 上。
  • 请解释具体的错误,如果有的话最好使用堆栈跟踪。

标签: python python-3.x python-2.x pdfminer


【解决方案1】:

file() 方法仅在 Python 2 中可用。

open() 方法在Python 2 中返回一个file 类型对象,在Python 3 中返回相同的对象。那里没有大的变化。

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2013-03-02
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多