首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用os.remove()删除所有电子邮件文件?通过电子邮件发送到TFS票证

如何使用os.remove()删除所有电子邮件文件?通过电子邮件发送到TFS票证
EN

Stack Overflow用户
提问于 2020-01-15 03:15:39
回答 1查看 110关注 0票数 1

我们目前使用TFS作为支持票证的票证系统。我编写了下面的代码,以便用户可以将电子邮件转储到文件夹中,然后访问网页以提交表单,创建TFS支持票证并将其分配给用户。目标是在提交表单并创建支持票证后,将电子邮件从文件夹中删除。

我遇到的问题是,当我在创建TFS支持票证后尝试删除电子邮件时,它会删除一些文件,但随后会卡在最后一个文件上,并显示以下错误消息。

“由于另一个进程正在使用该文件,因此进程无法访问该文件:'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg'”:PermissionError: WinError 32

如果我删除创建TFS票证的代码部分,那么它将重命名电子邮件,并在按预期完成后将其删除。似乎是TFS挂断了。我被难住了,需要帮助。

有什么想法吗?顺便说一句,这是我第一次尝试Python,所以要温文尔雅。:)

“”“

代码语言:javascript
复制
# -*- coding: utf-8 -*-

import extract_msg
import cgi
import json
from tfs import TFSAPI
import os, sys
import requests
import glob

import cgitb
cgitb.enable()

form = cgi.FieldStorage()
associate = form.getvalue('associate')

login = json.load(open('login.json'))
personal_access_token = login['tfs_login']
client = TFSAPI("https://url/", project="LoanSystems/", pat=personal_access_token)

os.chdir('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email')


def e2tfs():
    associate = form.getvalue('associate')
    i=1
    for file in os.listdir():
        src = file
        dst = "Delete_Me"+str(i)+".msg"
        os.rename(src,dst)
        msg = extract_msg.Message(dst)
        msg_sender = msg.sender
#        msg_date = msg.date
        msg_subj = msg.subject
        msg_message = msg.body
        i+=1

        fields = {'System.Title' : 'E2TFS: {}'.format(msg_subj),
                  'Microsoft.VSTS.CMMI.Symptom': 'Body: {}'.format(msg_message),
                  'Microsoft.VSTS.TCM.ReproSteps': 'TBD',
                  'Regions.Custom.DocumentationArea': 'Unknown',
                  'Regions.Custom.Application': 'nCino',
                  'Regions.Custom.Channel': 'Email',
                  'Microsoft.VSTS.CMMI.FoundInEnvironment': 'Production',
                  'Regions.Custom.ImpactedAssociate': 'Sender: {}'.format(msg_sender),
                  'Regions.Custom.Associate_Role': 'ALL USERS',
                  'Regions.Custom.BusinessGroupsImpacted2': 'All Business Groups',
                  'AFS.phase.dev': 'ALL USERS',
                  'Regions.Custom.PriorityCustomField': 'High',
                  'Regions.Custom.CaseOwner': associate          
                  }      


        client.create_workitem('Support Ticket', fields=fields)

        query_tfs = "SELECT [System.Id], [System.Title] FROM workitems WHERE [System.CreatedDate] = @today AND [System.CreatedBy] = @me AND [System.WorkItemType] = 'Support Ticket'"

        wiql = client.run_wiql(query_tfs)


        # Get all found workitems
        workitems = wiql.workitems
        tfs_number = workitems[-1]['Id']
        print(f"Support Ticket {tfs_number} was created.\n")

        #Create a link to the Support Ticket
        url = 'https://sfdctfs/tfs/LoanSystems/SalesForce%20COE/_workitems/edit/' + str(tfs_number)
        print("<a target=_blank href=\"" + url + "\"> Click Here to View in a New Tab</a></br></p>\r\n")


    filelist = glob.glob('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email\\*.msg')    
    for files in filelist:
#        print(f'{files} to be removed\n')
        os.remove(files)
#        print(f'{files} file was removed\n')




print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Email to TFS</title>")
print("</head>")
print("<body>")
print("<h2>Email to TFS</h2> <br />")
print('<a href=\"http://localhost:8000/cgi-bin/case.py\">Click here</a> if you need to create a "Support Ticket" from a "Case Assignment."<br /><br />')
print("1. Drag and drop the email(s) to the designated folder.<br />")
print('2. Select an Associate below to assign as the "Case Owner" on the "Support Ticket(s)."<br />')
print('3. Click the "Submit" button to generate a "Support Ticket(s)" in TFS. <br /><br />')
print("<form action =\"/cgi-bin/template.py\">")
print("Associate: <select name=\"associate\"> <br /> ")
print("<option value=\"--None--\">--None--</option>")
print("<option value=\"April\">April</option>")
print("<option value=\"Mac\">Mac</option>")
print("<option value=\"Michael\">Michael</option>")
print("<option value=\"Chris\">Chris</option>")
print("<input type = \"submit\" value = \"Submit\">")
print("</form><br />")
print("</body>")
print("</html>")
if associate != None:
    e2tfs()
print("<i>**Remember to remove the emails out of the directory when you're done.</i>")

“”“

我确实尝试在e2tfs()方法的底部添加close(),但收到以下错误:

Python脚本中出现问题。下面是导致错误的函数调用序列,按照它们发生的顺序。

()中的C:\Users\b4bw3\Documents\Python\simple-salesforce\cgi-bin\template2.py

代码语言:javascript
复制
     70 print("</html>")
     71 if associate != None:
=>   72     e2tfs()
     73 print("<i>**Remember to remove the emails out of the directory when you're done.</i>")
     74  

e2tfs()中的C:\Users\b4bw3\Documents\Python\simple-salesforce\cgi-bin\template2.py = e2tfs

代码语言:javascript
复制
     40     for files in filelist:
     41         print(f'{files} to be removed\n')
=>   42         files.close()
     43         os.remove(files)
     44         print(f'{files} file was removed\n')

r'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg',= files.close未定义的AttributeError:'str‘对象没有属性’r‘C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg’,‘args = ("'str’对象没有属性‘AttributeError’“,) with_traceback =

代码语言:javascript
复制
'''
    filelist = glob.glob('C:\\Users\\b4bw3\\Documents\\Python\\simple-salesforce\\Email\\*.msg')


    for files in filelist:
        print(f'{files} to be removed\n')
        files.close()
        os.remove(files)
        print(f'{files} file was removed\n')
'''
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 23:55:04

这似乎是msg-提取器的当前错误。我在下面找到了详细解释的链接。

https://github.com/mattgwwalker/msg-extractor/issues/85?_pjax=%23js-repo-pjax-container

我将下面的代码行添加到我的方法的末尾,它的效果很不错。

代码语言:javascript
复制
msg.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59740319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档