2015-11-07 47 views
0

我有一个小脚本,用于在将多页TIFF中的页计数移动到另一个驱动器之前进行计数;但我无论是否明确地用枕头他们打开后,要关闭图像,我不断收到在随后尝试移动文件PermissionError:在枕头中打开后移动图像时发生PermissionError

Traceback (most recent call last): 
    File "C:\Python34\lib\shutil.py", line 522, 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/barry/desktop/bort.tiff' -> 'c:/users/barry/desktop/bart.tiff' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<pyshell#75>", line 1, in <module> 
    shutil.move('c:/users/barry/desktop/bort.tiff', 'c:/users/barry/desktop/bart.tiff') 
    File "C:\Python34\lib\shutil.py", line 535, in move 
    os.unlink(src) 
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'c:/users/barry/desktop/bort.tiff' 

我已经试过每个以下方法I”,再加上其他的变化已经被遗忘:

import os 
import shutil 
from PIL import Image 

name1 = 'C:/Users/Barry/Desktop/bort.tiff' # This file exists. 
name2 = 'C:/Users/Barry/Desktop/bart.tiff' # This file doesn't. 

# Attempt 1: 
img = Image.open(name1) 
pgs = img.n_frames 
img.close()    # Tried both with and without this in each attempt. 
shutil.move(name1, name2) # Tried both shutil.move and os.rename 

# Attempt 2: 
with Image.open(name1) as img: 
    pgs = img.n_frames 
    img.close()   
shutil.move(name1, name2) 

# Attempt 3: 
with Image.open(name1) as img: 
    pgs = img.n_frames 
img.close()    
shutil.move(name1, name2) 

# Attempt 4: 
with open(name1, 'rb') as file: 
    with Image.open(file) as img: 
     pgs = img.n_frames 
img.close()    
shutil.move(name1, name2) 

即使包含在不同功能中的每个不同的操作没有区别。

我哪里错了?

我在Win7和Win10上都使用Pillow v3.0.0和Python v3.4.2。

回答

-1

你面对什么在这里是Sharing Violation Error

避免你应该使用Win32file.movefile进一步的信息,你应该go to

win32file.MoveFile(source, target) 

将做的工作。

+0

我不能为我的生活找出如何或从哪里获得win32file。但不幸的是,这个脚本也必须兼容Linux。 – BarryP

+0

[This is](http://sourceforge.net/projects/pywin32/files/)你会发现Win32file也可以找到[exe](http://starship.python.net/~skippy/downloads /win32all-163.exe),yes脚本将保持与Linux兼容的使用平台模块,然后根据平台架构应用您的代码。希望有所帮助。 –

+0

好了,没有工作: 回溯(最近通话最后一个): 文件 “”,1号线,在 win32file.MoveFile('C:/users/barry/desktop/bort.tiff ','d:/bart.tiff') pywintypes.error:(32,'MoveFile','进程无法访问文件 ,因为它正在被另一个进程使用') – BarryP