我想从许多tar.gz文件中提取具有模式Error creating Email. The remote server returned an error: (404) Not Found.
at System.Net.HttpWebRequest.GetResponse()
at OutSystems.HubEdition.RuntimePlatform.Email.EmailHelper.HttpGetContent(String ssUrl, String method, String contentType, String userAgent, Cookie cookie, QueryParameter[] parameters, String& ssContent, String& ssContentEncoding)
at OutSystems.HubEdition.RuntimePlatform.Email.EmailHelper.HttpPost(String ssUrl, QueryParameter[] parameters, String userAgent, Cookie cookie, String& ssContent, String& ssContentEncoding)
at OutSystems.HubEdition.RuntimePlatform.Email.EmailProcessor.SendEmailRequest(String url, String from, String to, String cc, String bcc, Int32 activityId, Int32 tenantId, Boolean storeContent, EmailType type) On
at OutSystems.HubEdition.RuntimePlatform.Email.EmailProcessor.SendEmailRequest(String url, String from, String to, String cc, String bcc, Int32 activityId, Int32 tenantId, Boolean storeContent, EmailType type)
at myEspace.Flows.FlowMainFlow.myScreen.CommandCreateOrder(HeContext heContext)
的所有文件,而不从档案中提取所有文件。
我找到了这些行,但无法使用通配符(https://pymotw.com/2/tarfile/):
*_sl_H*
有人有想法吗? 非常感谢提前。
答案 0 :(得分:7)
查看TarFile.getmembers()
方法,该方法将归档成员作为列表返回。获得此列表后,您可以根据条件决定要提取哪个文件。
import tarfile
import os
os.mkdir('outdir')
t = tarfile.open('example.tar', 'r')
for member in t.getmembers():
if "_sl_H" in member.name:
t.extract(member, "outdir")
print os.listdir('outdir')
答案 1 :(得分:2)
首先,您可以使用glob
来获取给定文件夹中*.tar
个文件的列表。然后在每个tar文件中获取成员列表并使用正则表达式过滤它们。然后将此列表传递给members
参数,如下所示:
import tarfile
import glob
import re
reT = re.compile(r'.*?_sl_H.*?')
for tar_filename in glob.glob(r'\my_source_folder\*.tar'):
try:
t = tarfile.open(tar_filename, 'r')
except IOError as e:
print e
else:
t.extractall('outdir', members=[m for m in t.getmembers() if reT.search(m.name)])