How to close pyPDF “PdfFileReader” Class file handle

前端 未结 4 1011
青春惊慌失措
青春惊慌失措 2021-01-12 01:12

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 sni

4条回答
  •  青春惊慌失措
    2021-01-12 01:55

    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')
    

提交回复
热议问题