【问题标题】:Python, WindowsError: [Error 32], file being used by another processPython,WindowsError:[错误 32],文件正在被另一个进程使用
【发布时间】:2016-06-28 16:23:43
【问题描述】:

我正在编写一个小程序,它应该遍历一个 msg 文件(即 MS Outlook 电子邮件)的文件夹,并在它们的文件名前加上一个短字符串。我一直在第 33 行 (os.rename(filenameOLD, filenameNEW)) 遇到 WindowsError: [Error 32] (The process cannot access the file because it is being used by another process)。知道为什么吗?

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
import os

path = 'C:\Users\MyName\Desktop\SomeFile\\'
msgFiles = os.listdir(path) # returns list

count = 0
for msgFile in msgFiles:
    count = count + 1

    msg = outlook.OpenSharedItem(path + msgFile)

    date = str(msg.SentOn)

    #Extract YYYY, MM, DD, HHMMSS from .msg sent date
    YYYY = str(20)+date.split("/")[2][:2]
    MM =  date.split("/")[1]
    DD =  date.split("/")[0]
    HHMMSS = "".join(date.split()[1].split(":")) + "Hrs"

    #Reformat date to valid file name    
    filenamePrefix =  YYYY + DD + MM + " " + HHMMSS + " "

    #generate new file name
    filenameOLD = path + msgFile
    filenameNEW = path + filenamePrefix + msgFile

    #rename file
    os.rename(filenameOLD, filenameNEW)

print count, "files renamed"

【问题讨论】:

  • 文件是否仍然从outlook.OpenSharedItem打开?
  • 可能是。不知道如何关闭它
  • date = str(msg.SentOn)之后做del msg
  • os.rename(filenameOLD, filenameNEW) 之前插入 del msg 就可以了。谢谢!

标签: python-2.7


【解决方案1】:

您打开了邮件但没有关闭它。而是这样做:

# ...
for msgFile in msgFiles:
    count = count + 1

    msg = outlook.OpenSharedItem(path + msgFile)
    date = str(msg.SentOn)
    del msg
# ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多