如何在API调用中过滤带有扩展名的文件名? [英] How to filter filenames with extension on API call?
问题描述
我正在使用python confluence API,用于从confluence页面下载附件,我只需要下载扩展名为 .mpp
的文件.尝试使用 glob
和直接参数,但是没有用.
I was working on the python confluence API for downloading attachment from confluence page, I need to download only files with .mpp
extension. Tried with glob
and direct parameters but didnt work.
这是我的代码:
file_name = glob.glob("*.mpp")
attachments_container = confluence.get_attachments_from_content(page_id=33110, start=0, limit=1,filename=file_name)
print(attachments_container)
attachments = attachments_container['results']
for attachment in attachments:
fname = attachment['title']
download_link = confluence.url + attachment['_links']['download']
r = requests.get(download_link, auth = HTTPBasicAuth(confluence.username,confluence.password))
if r.status_code == 200:
if not os.path.exists('phoenix'):
os.makedirs('phoenix')
fname = ".\\phoenix\\" +fname
推荐答案
glob.glob()
在您的 local 文件夹中运行.因此,您不能将其用作 get_attachments_from_content()
的过滤器.另外,请不要指定 limit
,因为这样只会使您获得第一个附件.指定一个上限,或者任何默认值都将包括所有这些上限.(您可能需要对结果进行分页.)
glob.glob()
operates on your local folder. So you can't use that as a filter for get_attachments_from_content()
. Also, don't specify a limit
of since that gets you just one/the first attachment. Specify a high limit or whatever default will include all of them. (You may have to paginate results.)
但是,您可以通过在下载每个附件之前检查它们的 title
来排除不需要的文件,这些文件的格式为 fname = attachment ['title']
.
However, you can exclude the files you don't want by checking the title
of each attachment before you download it, which you have as fname = attachment['title']
.
attachments_container = confluence.get_attachments_from_content(page_id=33110, limit=1000)
attachments = attachments_container['results']
for attachment in attachments:
fname = attachment['title']
if not fname.lower().endswith('.mpp'):
# skip file if it's not got that extension
continue
download_link = ...
# rest of your code here
此外,您的代码看起来像是从此答案中复制粘贴,但是您已更改了实际的下载";一部分.因此,如果您的下一个StackOverflow问题将是如何从融合中下载文件",请使用该答案的代码.
Also, your code looks like a copy-paste from this answer but you've changed the actual "downloading" part of it. So if your next StackOverflow question is going to be "how to download a file from confluence", use that answer's code.
这篇关于如何在API调用中过滤带有扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!