copy2 PermissionError:[WinError 32]进程无法访问该文件,因为它正由另一个进程使用:

时间:2017-01-18 02:42:09

标签: python

我正在将文件复制到临时目录

/opt/vtiger/config/config.inc.php

并希望在我上传后将其删除

def uploadAvatar(self, schid, img):
    try:
        img = path.abspath(img)
        filename = self.generateAvatarFileName(schid)
        temp_dir = tempfile.gettempdir()
        self.tmp = path.join(temp_dir, filename)
        copy2(img, self.tmp)
        #imgname = path.basename(self.tmp)
        imgdir = path.dirname(self.tmp)+path.sep
        self.retcode = ts3lib.createReturnCode()
        (error, ftid) = ts3lib.sendFile(schid, 0, "", "/avatar/"+filename, True, False, imgdir, self.retcode);
    except:
        try: from traceback import format_exc;ts3lib.logMessage(format_exc(), ts3defines.LogLevel.LogLevel_ERROR, "PyTSon Script", 0)
        except:
            try: from traceback import format_exc;print(format_exc())
            except: print("Unknown Error")

但我总是得到

def onServerErrorEvent(self, schid, errorMessage, error, returnCode, extraMessage):
    if self.retcode == returnCode: self.setAvatar(schid, self.tmp);remove(self.tmp)

怎么能做到这一点?请帮忙!

1 个答案:

答案 0 :(得分:2)

听起来你需要关闭文件或停止进程,这是我发现能够解决问题的链接。 File handling in Python - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process