Skip to content

windows only: pdfquery is locking the opended pdf-file #75

@iconberg

Description

@iconberg

I try open pdfs files to query data from it and then use that data to rename the pdf-file.
On windows this code fails with renaming cause the file is locked.
On linux the code is working.

I cannot see if this error belongs to pdfquery itself or an other module used by pdfquery is causing this.

import os
import pdfquery


def is_pdf(file):
    if os.path.splitext(file.lower())[1] == '.pdf':
        return True


pdf_files = os.listdir('./pages')
for pdf_file in filter(is_pdf, pdf_files):
    print(pdf_file)
    pdf = pdfquery.PDFQuery(os.path.join('pages', pdf_file))
    pdf.load()
    for e in pdf.tree.iter():
        text = e.text
        if text:
            text = text.replace(' ', '')
            if text[0:7] == '4002629':
                #del pdf
                os.rename(os.path.join('pages', pdf_file),
                          '{}.pdf'.format(text))
                break

Error on windows:

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\PDFs_aufbereiten\pdf_pages_rename.py", line 22, in <module>
    os.rename(os.path.join('pages', pdf_file), '{}.pdf'.format(text))
PermissionError: [WinError 32] Der Prozess kann nicht auf die Datei zugreifen, da sie von einem anderen Prozess verwendet wird: 'pages\\xxxxxxxxxxxxxxxxxxxx.pdf' -> 'xxxxxxxxxxxxx.pdf'

Code on linux is working.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions