使用python从artifactory下载.xml文件时,正在下载损坏的xml

2024-09-23 06:35:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的脚本下载artifactory目录中的所有xml文件(子文件夹名包含各种.xml文件)

from artifactory import ArtifactoryPath
import shutil,os,requests

url="https://artifactory.xxx.com/artifactory/foldername/subfoldername/"
logdirectory='C:\\subfoldername'
if not os.path.exists(logdirectory):
            os.mkdir(logdirectory)
os.chdir(logdirectory)
xmlfiles = ArtifactoryPath(url,auth=(username, password))
for element in xmlfiles:
    print (element )
    xmlname=str(element).split("/")[-1]
    print(xmlname)
    with requests.get(element , auth=(username, password), stream=True) as r:
         with open(xmlname, 'wb') as f:
         shutil.copyfileobj(r.raw, f)

它正在下载具有正确名称的xml文件,但当我尝试打开这些文件时,它会显示损坏的数据。 enter image description here

PS:我也尝试了(https://pypi.org/project/artifactory/)上的以下方法,但它也使用我的路径和身份验证代码生成了相同的损坏xml。粘贴在下面的代码供快速参考

from artifactory import ArtifactoryPath
path = ArtifactoryPath(
    "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz")

with path.open() as fd:
    with open("tomcat.tar.gz", "wb") as out:
        out.write(fd.read())

请建议如何下载xml文件而不使其损坏


Tags: 文件pathorgimportosaswithxml
1条回答
网友
1楼 · 发布于 2024-09-23 06:35:45

类似这样的工作原理:

for element in xmlfiles:
    print(element)
    xmlname= str(element).split("/")[-1]
    print(xmlname)
    req = requests.get(element , auth=(username, password))
         with open(xmlname, 'wb') as f:
            f.write(req.content)

在制品

相关问题 更多 >