gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:33 26 4
gpt4 key购买 nike

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

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

“PermissionError:[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>\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

     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 = e2tfs() 中的 C:\Users\b4bw3\Documents\Python\simple-salesforce\cgi-bin\template2.py

     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')
'''

最佳答案

看来这是 msg-extractor 的当前错误。我找到了下面的链接,其中详细解释了。

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

我将以下代码行添加到我的方法的末尾,它的工作方式就像一个魅力。

msg.close()

关于python - 如何使用 os.remove() 删除所有电子邮件文件?通过电子邮件发送至 TFS 票证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59740319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com