如何 print/list 只有 OS.Walk 中的 5 个条目?

How to print/list only 5 entries from OS.Walk?

问题

我之前发布了这个问题(此处:),然后它起作用了。现在,突然之间,它只显示了 5 个结果,然后又开始向我显示所有结果。

目标:

使用 OS 步行时仅列出 5 个条目。到目前为止,我只能获得使用 OS.Walk 找到的所有内容的列表,或者只能列出一个条目。 我正在使用:

for file in files[0:5]: #You show the first five only

获得前五个结果,而不是再次给我所有结果。

完整代码如下:

import os
import subprocess


def playmusic(name):
    for root, dirs, files in os.walk('E:\', followlinks=True):
        for file in files[0:5]: #You show the first five only
            if name in file:
                vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                music=str(os.path.join(root,file))
                print(music)
                #subprocess.Popen([vlc, music])
                #return
    print("Finish")
    input()
try:
    s=raw_input("name: ")
    playmusic(s)
except Exception as e:
    print(e)
    print("Error")

这是结果

name: te
E:\PC Games\Assassin's Creed\Detection\Detection.exe
E:\PC Games\Assassin's Creed\Detection\detectionapi_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx10tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx9tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directxtests_rd.tst
E:\PC Games\Assassin's Creed\Detection\localization\CZ\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\DEU\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\EN\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\ESM\interpreter_local.ini

more results than what is show here...

我尝试了一些不同的事情。

喜欢用计数器:

import os
import subprocess


def playmusic(name):
    count = 0
    for root, dirs, files in os.walk('E:\', followlinks=True):
        for file in files:
            if name in file:
                vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                music=str(os.path.join(root,file))
                print(music)
                count = count + 1 
                if count == 5:
                    break
                #subprocess.Popen([vlc, music])
                #return
    print("Finish")
    input()
try:
    s=raw_input("name: ")
    playmusic(s)
except Exception as e:
    print(e)
    print("Error")

结果:

name: te
E:\PC Games\Assassin's Creed\Detection\Detection.exe
E:\PC Games\Assassin's Creed\Detection\detectionapi_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx10tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx9tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directxtests_rd.tst
E:\PC Games\Assassin's Creed\Detection\localization\CZ\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\DEU\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\EN\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\ESM\interpreter_local.ini

more results than what is shown here...

或尝试 range:

import os
import subprocess


def playmusic(name):
    for i in range(5):
        for root, dirs, files in os.walk('E:\', followlinks=True):
            for file in files:
                if name in file:
                    vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                    music=str(os.path.join(root,file))
                    print(music)
                    i += 1
                    if i == 5:
                        break
                    #subprocess.Popen([vlc, music])
                    #return
    print("Finish")
    input()
try:
    s=raw_input("name: ")
    playmusic(s)
except Exception as e:
    print(e)
    print("Error")

结果:

name: te
E:\PC Games\Assassin's Creed\Detection\Detection.exe
E:\PC Games\Assassin's Creed\Detection\detectionapi_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx10tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directx9tests_rd.dll
E:\PC Games\Assassin's Creed\Detection\directxtests_rd.tst
E:\PC Games\Assassin's Creed\Detection\localization\CZ\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\DEU\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\EN\interpreter_local.ini
E:\PC Games\Assassin's Creed\Detection\localization\ESM\interpreter_local.ini

more results than what is show here...

不过,我得到的结果多于五个。

是否有任何原因导致代码停止工作?比如缩进之类的?还是我错过了什么?我整个周末都在调查这个问题,到目前为止还没有结果。

此外,如果有人有任何其他很棒的想法或方法。

研究

https://www.w3resource.com/python/python-break-continue.php

它正在按照您的要求进行操作。它显示每个目录中的 5 个文件,然后移动到每个子目录中,每个子目录中显示 5 个文件。如果你想让它在处理 5 行后停止一切,你需要退出两个循环。

import os
import subprocess

def playmusic(name):
    count = 0
    for root, dirs, files in os.walk('E:\', followlinks=True):
        for file in files:
            if name in file:
                vlc='C:/Program Files/VideoLAN/VLC/vlc.exe'
                music=str(os.path.join(root,file))
                print(music)
                count = count + 1 
                if count == 5:
                    break
        if count >= 5:
            break
    print("Finish")