【问题标题】:WinError 32 :The process cannot access the file because it is being used by another processWinError 32:该进程无法访问该文件,因为它正在被另一个进程使用
【发布时间】:2018-10-28 13:08:21
【问题描述】:

我编写了以下代码来提取目录中的 zip 文件并删除提取目录中的特定 excel 文件:

def extractZipFiles(dest_directory):
    "This function extracts zip files in the destination directory for further processing"
    fileFullPath = dest_directory + '\\'
    extractedDirList = list()
    for file in os.listdir(dest_directory):
        dn = fileFullPath+file
        dn = re.sub(r'\.zip$', "", fileFullPath+file) #remove the trailing .zip.
        extractedDirList.append(dn)
        zf = zipfile.ZipFile(fileFullPath+file, mode='r')
        zf.extractall(dn) # extract the contents of that zip to the empty directory
        zf.close()
    return extractedDirList

def removeSelectedReports(extractedDirList):
    "This function removes the selected reports from extracted directory"
    for i in range(len(extractedDirList)):
            for filename in os.listdir(extractedDirList[i]):
                if filename.startswith("ABC_8"):
                        logger.info("File to be removed::"+filename)
                        fullPathName= "%s/%s" % (extractedDirList[i],filename)
                        os.remove(fullPathName)
    return

extractedDirList = extractZipFiles(attributionRptDestDir)
logger.info("ZIP FILES EXTRACTED:"+str(extractedDirList))
removeSelectedReports(extractedDirList)

即使我已关闭 zip 文件处理程序,我仍会遇到以下间歇性问题。

[WinError 32] The process cannot access the file because it is being used by another process: '\\\\share\\Workingdirectory\\report.20180517.zip'

你能帮忙解决这个问题吗

【问题讨论】:

  • 请完整错误堆栈。
  • 我在日志文件中除了 WinError32 没有任何错误

标签: python python-3.x


【解决方案1】:

您应该尝试找出打开的文件是什么。根据您的代码,您似乎使用的是 Microsoft Windows。

我会停止您工作站上的所有应用程序,包括浏览器,只在打开最少数量的应用程序的情况下运行,然后重现问题。复制后,您可以使用工具列出打开特定文件的所有句柄。

一个方便的实用程序是handle.exe,但请使用任何具有类似功能的工具。

一旦发现有问题的应用程序,您可以进一步调查文件打开的原因,并采取应对措施。

我会注意不要关闭任何打开文件的应用程序,直到您知道这样做是安全的。

【讨论】:

  • 我只检查了 1 个 python 进程正在运行
  • 当然,但这不能回答问题,也不符合我给出的答案;你机器上的其他进程怎么样......他们可能有一个打开你文件的句柄...... ??
  • 我在 psutil.process_iter() 代码中为 p 执行以列出正在运行的进程。但是如何找到对我的文件有句柄的进程呢?
  • @Karthik 你运行了 handle.exe 吗?
  • 我在 autosys VM 上执行这个脚本。该机器上没有安装句柄。有没有其他方法可以在python中找到相同的?
猜你喜欢
  • 2015-11-13
  • 2015-01-28
  • 2017-06-02
  • 2021-09-21
  • 2019-11-26
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2020-10-31
相关资源
最近更新 更多