python 重命名目录中的所有PDF时出现权限错误

li9yvcax  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(143)

我正在创建一个程序,该程序将根据内容在特定目录中重命名一系列PDF文件。我已经将内容提取到一个字符串中,但是os.rename()无法更改名称,因为文件已经打开。我找到了a nearly identical solution,但我还不能正确实现它。我感觉我离完美的功能已经很近了,但我不知道该把load_pdf.close()放在哪里,也不知道是否需要换一种说法,无论放在哪里,要么抛出相同的错误,要么抛出其他错误,从而导致一定的失败。

import PyPDF2
import os

for file_name in os.listdir('upload_12.5.22_test'):
    load_pdf = open('C:/Users/Jake/Documents/upload_12.5.22_test/'+file_name,'rb')
    read_pdf = PyPDF2.PdfFileReader(load_pdf)
    page_count = read_pdf.getNumPages()
    data_page = read_pdf.getPage(0)
    page_content = data_page.extractText()
    page_content = page_content.replace('\n','')
    page_content = page_content.split('reports.')
    del page_content[0:1]
    p_c_str = ''.join(page_content)
    p_c_str = p_c_str.strip()
    p_c_str = p_c_str[:-6]
    p_c_str = p_c_str + " agreement"
    load_pdf.close()
    os.rename('C:/Users/Jake/Documents/upload_12.5.22_test/'+file_name, 'C:/Users/Jake/Documents/upload_12.5.22_test/'+p_c_str+".pdf")

错误:

Traceback (most recent call last):
  File "C:\Users\Jake\Documents\progam1.py", line 30, in <module>
    os.rename('C:/Users/Jake/Documents/upload_12.5.22_test/'+file_name, 'C:/Users/Jake/Documents/upload_12.5.22_test/'+p_c_str+".pdf")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/Jake/Documents/upload_12.5.22_test/First M Last agreement.pdf' -> 'C:/Users/Jake/Documents/upload_12.5.22_test/First Last agreement.pdf'
[Finished in 456ms]

**UPDATE:**我找到了this solution,它似乎解决了这个问题。所以,我不正确地调用了os.rename(),并试图更改活动文件目录。所以,我已经将所有内容移到了一个目录中,以消除链接到直接源代码时出现的任何错误,但它仍然抛出完全相同的错误,所以我回到了开始的地方。

import PyPDF2
import os

for file_name in os.listdir():
    load_pdf = open(file_name,'rb')
    read_pdf = PyPDF2.PdfFileReader(load_pdf)
    page_count = read_pdf.getNumPages()
    data_page = read_pdf.getPage(0)
    page_content = data_page.extractText()
    page_content = page_content.replace('\n','')
    page_content = page_content.split('reports.')
    del page_content[0:1]
    p_c_str = ''.join(page_content)
    p_c_str = p_c_str.strip()
    p_c_str = p_c_str[:-6]
    p_c_str = p_c_str + " agreement"
    load_pdf.close()
    os.rename(file_name, p_c_str+".pdf")

错误:

Traceback (most recent call last):
  File "C:\Users\Jake\Documents\Work Projects\Python\Contract Extraction\upload_12.5.22_test\contract_extraction_testing_2.py", line 28, in <module>
    os.rename(file_name, p_c_str+".pdf")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Alan R Diaz agreement.pdf' -> 'Alan Diaz agreement.pdf'
[Finished in 205ms]
91zkwejq

91zkwejq1#

我的建议是使用上下文管理器,它会在您阅读完文件后立即关闭文件:

with open('C:/Users/Jake/Documents/upload_12.5.22_test/'+file_name,'rb') as load_pdf:
    read_pdf = PyPDF2.PdfFileReader(load_pdf)

然而,你的代码应该是正确的,因为你在尝试重命名之前关闭了文件。正如其他人在评论中所说的,如果文件在任何其他进程中打开,Windows不会让你重命名它,所以最有可能的解决方案是关闭其他应用程序,甚至可能重新启动。

cfh9epnr

cfh9epnr2#

感谢这里的建议,我真的很感激你的帮助。我能够让它在一个VBox与Fedora工作,以消除任何Windows恶作剧,我相信这将有利于我未来的编码努力以及。我正在运行的最终版本如下:

from PyPDF2 import PdfMerger, PdfReader
import PyPDF2
import os

today = '/upload 12.9.22/'

for file_name in os.listdir('/home/jake/Documents/WEEKLY_UPLOAD'+today):
    read_pdf = PyPDF2.PdfReader('/home/jake/Documents/WEEKLY_UPLOAD'+today+file_name,'rb')
    page_count = read_pdf.getNumPages()
    data_page = read_pdf.getPage(0)
    page_content = data_page.extractText()
    page_content = page_content.replace('\n','')
    page_content = page_content.split('reports.')
    del page_content[0:1]
    p_c_str = ''.join(page_content)
    p_c_str = p_c_str.strip()
    p_c_str = p_c_str[:-6]
    new_name = p_c_str + " agreement.pdf"
    os.rename('/home/jake/Documents/WEEKLY_UPLOAD'+today+file_name, '/home/jake/Documents/WEEKLY_UPLOAD'+today+new_name)

相关问题