如何关闭 pyPDF“PdfFileReader”类文件句柄

发布于 2024-10-07 21:07:37 字数 336 浏览 0 评论 0原文

这应该是一个非常简单的问题,我无法通过谷歌搜索找到答案:How to close file handle opening by pyPDF "PdfFileReader" Class

Here is snippet:

import os.path
from pyPdf import PdfFileReader

fname = 'my.pdf'
input = PdfFileReader(file(fname, "rb"))

os.rename(fname, 'my_renamed.pdf')

which raises error [32]

谢谢

this should be very simple question, for which I couldn't find answer by Google search: How to close file handle opened by pyPDF "PdfFileReader" Class

Here is snippet:

import os.path
from pyPdf import PdfFileReader

fname = 'my.pdf'
input = PdfFileReader(file(fname, "rb"))

os.rename(fname, 'my_renamed.pdf')

which raises error [32]

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贱人配狗天长地久 2024-10-14 21:07:37

操作系统正在阻止文件在其他程序打开时被重命名。这是一件好事(tm)。

Python 的 with 语句会在您执行完操作后自动关闭文件。完成阅读/操作。

with open(fname, "rb") as f:
  input = PdfFileReader(f, "rb")

os.rename(fname, 'my_renamed.pdf')

如果您仍在使用 Python 2.5,则必须执行特殊导入:

from __future__ import with_statement

Python 2.6 及更高版本默认启用 with 。

The operating system is preventing a file from being re-named while something else has it open. This is a Good Thing (tm).

Python's with statement will automatically close the file after you're done reading/manipulating it.

with open(fname, "rb") as f:
  input = PdfFileReader(f, "rb")

os.rename(fname, 'my_renamed.pdf')

If you're still on Python 2.5, you'll have to do a special import:

from __future__ import with_statement

Python 2.6 and above have with enabled by default.

ぃ弥猫深巷。 2024-10-14 21:07:37

如果您确实必须从 PdfFileReader 对象访问它(即:如果您自己没有对该文件对象的引用),则可以使用 reader.stream.close()

请注意,PdfFileReader 需要一个打开的文件对象来访问 pdf 的内容(它不会从一开始就将所有内容拉入内存),因此只有在使用完阅读器后才关闭文件。

If you really have to access this from the PdfFileReader object (that is: if you haven't got a reference to the file object yourself), you can use reader.stream.close()

Note that the PdfFileReader will need an open file object to access the pdf's content (it doesn't pull everything into memory from the start), so only close the file when you are done with the reader.

烂柯人 2024-10-14 21:07:37

我建议处理从 PdfFileReader 打开的文件

您的代码将是:

import os.path
from pyPdf import PdfFileReader

fname = 'my.pdf'
fh = file(fname, "rb")
input = PdfFileReader(fh)

fh.close()
os.rename(fname, 'my_renamed.pdf')

I would sugest to handle the file open out of the PdfFileReader

Your code will be:

import os.path
from pyPdf import PdfFileReader

fname = 'my.pdf'
fh = file(fname, "rb")
input = PdfFileReader(fh)

fh.close()
os.rename(fname, 'my_renamed.pdf')
深海里的那抹蓝 2024-10-14 21:07:37

而是使用 input=PdfFileReader(file(fname, "rb"))
创建一个像这样的输入流

inputStream=file(fname, "rb")
    input=PdfFileReader(inputStream)

,当工作完成时使用 inputStream.close()
然后你就可以通过 os 包调用它

instead using input=PdfFileReader(file(fname, "rb"))
create an input stream like this

inputStream=file(fname, "rb")
    input=PdfFileReader(inputStream)

and when job is done use inputStream.close()
then u will be able to call it through os package

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文