如何使用 os.remove() 删除所有电子邮件文件?通过电子邮件发送至 TFS 票证
How do I remove ALL email files using os.remove() ? Email to TFS ticket
我们目前使用 TFS 作为支持工单的工单系统。我编写了下面的代码,以便用户可以将电子邮件转储到文件夹中,然后访问网页以提交表单并创建 TFS 支持票证并将其分配给用户。目标是在提交表单并创建支持票证后从文件夹中删除电子邮件。
我 运行 遇到的问题是,当我在创建 TFS 支持票证后尝试删除电子邮件时,它删除了一些文件,但随后卡在最后一个文件上并显示以下错误消息。
"PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '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>\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 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 在 e2tfs()
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')
files = r'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg', files.close 未定义
AttributeError: 'str' 对象没有属性 'close'
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 removed\n')
files.close()
os.remove(files)
print(f'{files} file was removed\n')
'''
这似乎是味精提取器的当前错误。我在下面找到了详细解释的 link。
https://github.com/mattgwwalker/msg-extractor/issues/85?_pjax=%23js-repo-pjax-container
我在我的方法末尾添加了以下代码行,效果非常好。
msg.close()
我们目前使用 TFS 作为支持工单的工单系统。我编写了下面的代码,以便用户可以将电子邮件转储到文件夹中,然后访问网页以提交表单并创建 TFS 支持票证并将其分配给用户。目标是在提交表单并创建支持票证后从文件夹中删除电子邮件。
我 运行 遇到的问题是,当我在创建 TFS 支持票证后尝试删除电子邮件时,它删除了一些文件,但随后卡在最后一个文件上并显示以下错误消息。
"PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '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>\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 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 在 e2tfs()
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')
files = r'C:\Users\b4bw3\Documents\Python\simple-salesforce\Email\Delete_Me1.msg', files.close 未定义 AttributeError: 'str' 对象没有属性 'close' 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 removed\n')
files.close()
os.remove(files)
print(f'{files} file was removed\n')
'''
这似乎是味精提取器的当前错误。我在下面找到了详细解释的 link。
https://github.com/mattgwwalker/msg-extractor/issues/85?_pjax=%23js-repo-pjax-container
我在我的方法末尾添加了以下代码行,效果非常好。
msg.close()