【问题标题】:How to print/list only 5 entries from OS.Walk?如何从 OS.Walk 仅打印/列出 5 个条目?
【发布时间】:2021-03-08 01:22:40
【问题描述】:

问题

我不久前发布了这个问题(这里:print/list only 5 entries from OS.Walk in python),然后它就起作用了。现在,不知从何而来,它不再只显示 5 个结果,而是重新开始向我显示所有内容。

目标:

使用 OS walk 时仅列出 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
End loop with counter and condition

【问题讨论】:

    标签: python python-2.7 for-loop count os.walk


    【解决方案1】:

    它完全按照你的要求做。它向您显示每个目录中的 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")
    

    【讨论】:

    • 天哪,这成功了!我认为我在另一个 stackoverflow 问题中是这样的,这个人没有打破两个循环。我尝试再放一次休息,但我没有放 if count >= 5: 第二次休息。这很有意义,因为我必须打破两个循环。
    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2016-05-31
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多