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



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

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

"权限错误: [WinError 32] 进程无法访问该文件,因为它正被另一个进程使用:"C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg'">

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

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

'''

# -*- 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>rn")

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


print("Content-type:text/htmlrnrn")
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 in ((

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 in e2tfs((

40     for files in filelist:
41         print(f'{files} to be removedn')
=>   42         files.close()
43         os.remove(files)
44         print(f'{files} file was removedn')

files= r'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg', files.close undefined 属性错误:"str"对象没有属性"关闭" args = ("'str' 对象没有属性 'close'",( with_traceback =

'''
filelist = glob.glob('C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\*.msg')

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

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

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

我在方法的末尾添加了以下代码行,它就像一个魅力。

msg.close()

相关内容

  • 没有找到相关文章

最新更新