在for循环之后,我只打印到图像文件夹的一个字符串路径,而不是多个图像字符串,python

2024-10-04 01:26:58 发布

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

我已经使用os.walk()函数迭代了文件夹媒体/图像,其中包含指向该文件夹图像的所有路径。在我完成了in-loop之后,我在那个文件夹中只得到了一张图片,我认为Dota2英雄的图片超过了115张

我将向您展示代码和打印屏幕,以便您可以仔细查看。我想我在循环中犯了一个错误,但我对python还不熟悉

插入_herones.py(第15行到第18行是在“我的媒体/图像”文件夹中迭代的一行。)

import requests
import json
import os

API_URL = 'http://127.0.0.1:8000'

if __name__ == '__main__':
    r = requests.get("https://api.opendota.com/api/heroes")
    all_heroes_info = json.loads(r.content.decode('UTF-8'))

    for hero_info in all_heroes_info:
        name = hero_info['localized_name']
        hero_type = hero_info['primary_attr']

    for root, dirs, files in os.walk("../../media/images/", topdown=False):
        for image in files:
            pass
        print(image)

        keys = [name]
        values = [image]
        # dictionary = dist(zip(keys, values))
        # print(dictionary)
        # print(keys)

        mutation_create_hero = '''
                  mutation ($name: String!, $heroType: String!, $image: String!) {
                      createHero(name: $name, heroType: $heroType, image: $image) {
                          name
                          image
                      }
                  }
              '''
        variables = {'name': name, 'heroType': hero_type, 'image': image}

        # localhost_api_response = requests.post(
        #     '{}/graphql/'.format(API_URL),
        #     json={
        #         'query': mutation_create_hero,
        #         'variables': variables
        #     }
        # )

我得到的是,当我打印图像变量--https://prnt.sc/qmzh8g 只有一个图标。Zeus icon.png,是媒体/图像目录中的最后一个图像文件

谢谢


Tags: namein图像imageimportinfo文件夹api
1条回答
网友
1楼 · 发布于 2024-10-04 01:26:58

将第15-18行从

for root, dirs, files in os.walk("../../media/images/", topdown=False):
    for image in files:
        pass
    print(image)

致:

for root, dirs, files in os.walk("../../media/images/", topdown=False):
    for image in files:
        print(image)

你的压痕在印刷品上有点错位。print语句在循环中,但在内部循环之外

相关问题 更多 >