繁体   English   中英

Python WinError 32 进程无法访问该文件,因为它正被另一个进程使用

[英]Python WinError 32 The process cannot access the file because it is being used by another process

我制作了一个脚本,将 pdf 文件转换为 jpg,然后将这些 jpg 放在特定文件夹中。 到目前为止,该脚本运行良好,但是我在 VScode 终端中不断收到错误消息:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/'

我尝试在运行之前关闭文件,但它仍然可以

from pdf2image import convert_from_path
import glob, os
import os, subprocess
import shutil

pdfPath = "C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/"
os.chdir(pdfPath)

for pdf_file in glob.glob(os.path.join(pdfPath, "*.pdf")): #converts PDF files from pdfPath then 
#converts
pages = convert_from_path(pdf_file, 500)   
for page in pages:       
    page.save(pdf_file[:-4] +".jpg", 'JPEG')

for i in os.listdir(pdfPath): #removes orginal PDF files
    if i.endswith('.pdf'):
        os.remove(i)

jpgPath = "C:/Users/xxx/Desktop/pdf2jpg/ConvertedFilesJPG/"

fileNum = 0
foldNum = 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
for base, dirs, files in os.walk(jpgPath): #checks number of folders in the destination path   
for directories in dirs:
    foldNum += 1                       
for Files in files:
    fileNum += 1
print(foldNum) #for debugging

if os.path.exists(jpgPath): 
    strNum = foldNum + 1
    fStrNum = str(strNum)
    strJpgPath = jpgPath + "Files" + fStrNum + "/"  #adds number of files in path, so new folders can 
    #be organized by name 
    shutil.move(pdfPath, strJpgPath)
Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 806, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/' -> 'C:/Users/xxx/Desktop/pdf2jpg/ConvertedFilesJPG/Files4/'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\xxx\Desktop\pdf2jpg\src_files\script.py", line 33, in <module>
    shutil.move(pdfPath, strJpgPath)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 824, in move
    rmtree(src)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 622, in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 620, in _rmtree_unsafe
    os.rmdir(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/xxx/Desktop/pdf2jpg/src_files/Files/'
PS C:\Users\xxx\Desktop\pdf2jpg>

你的脚本从做开始

os.chdir(pdfPath)

所以运行你的脚本的进程是持有pdfPath目录的进程,并防止它被shutil.move删除。 在移动它之前在其他地方做一个chdir ,例如:

chdir("C:/Users/xxx/Desktop/pdf2jpg/")
if os.path.exists(jpgPath):
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM