2015-11-07 45 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