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

The problem

I posted this question a bit ago (Here: print/list only 5 entries from OS.Walk in python) and it worked then. Now, out of nowhere, it stopped only showing only 5 results and started showing me everything again.

The Goal:

To list only 5 entries when using OS walk. So far I have only been able to get a list of everything that I find using OS.Walk or only list one entry. I was using:

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

to get the first five results and not it’s giving me all the results again.

Here is the full code:

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")

Here is the result

name: te
E:PC GamesAssassin's CreedDetectionDetection.exe
E:PC GamesAssassin's CreedDetectiondetectionapi_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx10tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx9tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectxtests_rd.tst
E:PC GamesAssassin's CreedDetectionlocalizationCZinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationDEUinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationENinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationESMinterpreter_local.ini

more results than what is show here...

I tried doing a couple of different things.

Like using a counter:

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")

Result:

name: te
E:PC GamesAssassin's CreedDetectionDetection.exe
E:PC GamesAssassin's CreedDetectiondetectionapi_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx10tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx9tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectxtests_rd.tst
E:PC GamesAssassin's CreedDetectionlocalizationCZinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationDEUinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationENinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationESMinterpreter_local.ini

more results than what is shown here...

Or trying 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")

Result:

name: te
E:PC GamesAssassin's CreedDetectionDetection.exe
E:PC GamesAssassin's CreedDetectiondetectionapi_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx10tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectx9tests_rd.dll
E:PC GamesAssassin's CreedDetectiondirectxtests_rd.tst
E:PC GamesAssassin's CreedDetectionlocalizationCZinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationDEUinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationENinterpreter_local.ini
E:PC GamesAssassin's CreedDetectionlocalizationESMinterpreter_local.ini

more results than what is show here...

Still, I get more results than five.

Is there any reason why the code would stop working? Like indentation or something? Or did I miss something? I have been looking into this all weekend and with no results so far.

Also if someone has any other ideas or methods that would be awesome.

Research

https://www.w3resource.com/python/python-break-continue.php
End loop with counter and condition

Answer

It is doing exactly what you asked. It is showing you 5 files from each directory, and then moves into each subdirectory, where it shows you 5 from each. If you want it to stop everything after handling 5 lines, you need to exit both loops.

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")

Leave a Reply

Your email address will not be published. Required fields are marked *