【问题标题】:Reading a directory for files then displaying them on a pygame surface读取文件目录,然后在 pygame 表面上显示它们
【发布时间】:2018-11-14 07:21:34
【问题描述】:

我有一个函数可以读取名为“游戏”的目录并在其中查找某些文件。这部分工作正常。问题是我想计算有多少个文件,每个文件我想将 10 添加到一个变量中。

def games():
    f = 0
    sy = 100
    ftext = pygame.font.SysFont("Arial", 20)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        screen.fill(DarkSpace)
        ToolBarButton("Home", 0, 0, 150, 50, SpaceGrey, CornflowerBlue, 10, cmain)
        ToolBarButton(username, 153,0,150,50, SpaceGrey, CornflowerBlue, 10, accountDetails)
        ToolBarButton("Programs", 305,0,150,50, SpaceGrey, CornflowerBlue, 10, programs)
        ToolBarButton("Games", 458,0,150,50, SpaceGrey, CornflowerBlue, 10, games)
        ToolBarButton("Help", 610,0,150,50, SpaceGrey, CornflowerBlue, 10, hel)
        DropDown(NeonGreen, CornflowerBlue, 764, 16, 30, 30, DropMenu)
        Btext(screen, "Loading Games!", CornflowerBlue, ftext, 600,600,600,600)
        fileDir = os.listdir(r"D:\Users\26099\Desktop\Programming\Dark_Dragons\Blunt_Wars1\venv\Launcher\Games")
        nf = len(fileDir)
        while nf > f:
            f += 1
            sy += 10
            break
        print(sy)
        for fileN in fileDir:
            verif = fileN.endswith('.py') or fileN.endswith('.pyw')
            if not verif:
                fileDir.remove(fileN)
            else:
                print(fileN)
                text(screen, fileN, CornflowerBlue, ftext, 300,sy)
        pygame.display.update()

所以我想为每个具有特定扩展名的文件添加 100 到 sy。我尝试将其放入 for 循环中,但出现错误说 int object is not callable 所以有人知道如何处理这个

【问题讨论】:

    标签: python-3.x file variables pygame


    【解决方案1】:

    要获取目录中的文件数,您可以这样做(这称为“列表理解”,类似于将文件附加到空列表的for 循环):

    files = [f for f in os.listdir() if os.path.isfile(f)]
    num_files = len(files)
    

    如果你想计算特定的扩展名,你可以这样做(最好使用字典而不是单独的变量):

    py_files = 0
    pyw_files = 0
    for file_name in os.listdir():
        if file_name.endswith('.py'):
            py_files += 1
        elif file_name.endswith('.pyw'):
            pyw_files += 1
    

    或者以更复杂的方式使用collections.Counter

    import collections
    
    extensions = collections.Counter(
        os.path.splitext(f)[1] for f in os.listdir() if os.path.isfile(f))
    print(extensions, sum(extensions.values()))
    

    补充说明:

    在迭代列表时不要修改列表。这会导致意想不到的结果。而是创建一个新的过滤列表。

    while nf > f: 循环看起来毫无意义。您可以将nf 乘以10sy = nf * 10。另外,因为您调用了break,所以您在第一次迭代后结束了循环。

    您可能不必每帧重新计算文件,每秒数百次,所以我会在 while 循环之前或发生事件时执行一次。

    【讨论】:

    • 这有点帮助,但它不能回答我的问题。我想知道热查找具有该扩展名的所有文件,然后将其打印到控制台。这对我来说真的不起作用,因为它显示的文件甚至不在我的文件夹中。
    • 如果脚本不在同一目录中,您必须将正确的路径传递给os.listdir。您想获取实际文件还是仅获取文件数?如果您想要数字,您可以使用collections.Counter 解决方案并编写例如print(extensions['.py']) 来打印.py 文件的数量。
    【解决方案2】:

    使用 os.walk。

    x = [_ for _ in os.walk("../source")][0]
    
    # index-10-> file path
    # index-1 -> folders in directory
    # index-2 -> files in directory
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2023-03-21
      • 2021-07-21
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多