Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中仅打印/列出OS.Walk中的5个条目_Python_List_Os.walk - Fatal编程技术网

在python中仅打印/列出OS.Walk中的5个条目

在python中仅打印/列出OS.Walk中的5个条目,python,list,os.walk,Python,List,Os.walk,我的目标-在使用OS walk时只列出5个条目。到目前为止,我只能得到一个使用OS.Walk找到的所有东西的列表,或者只列出一个条目。(通过使用返回函数) 我的代码: 导入操作系统 导入子流程 def playmusic(name): for root, dirs, files in os.walk('E:\\', followlinks=True): for file in files: if name in file:

我的目标-在使用OS walk时只列出5个条目。到目前为止,我只能得到一个使用OS.Walk找到的所有东西的列表,或者只列出一个条目。(通过使用返回函数)

我的代码: 导入操作系统 导入子流程

def playmusic(name):
    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)
                #subprocess.Popen([vlc, music])
                #return
    print("Finish")
    input()
try:
    s=raw_input("name: ")
    playmusic(s)
except Exception as e:
    print(e)
    print("Error")
结果:

=== RESTART: C:\Users\VGMPC2\Documents\testing scripts\search and print.py ===
name: test
E:\Nes\Jordan Vs Bird\3 Point Contest.mp4
E:\Nes\Jordan Vs Bird\Slam Dunk Contest.mp4
E:\playlist\Action&Adventuretest.xspf
E:\playlist\schedule test 2.xspf
E:\Snes\Lufia II\The Greatest Thieves.mp4
E:\Symbolic playlists\Nintendo Generation\3 Point Contest.mp4
E:\Symbolic playlists\Nintendo Generation\Slam Dunk Contest.mp4
Finish

如果有任何方法只显示5而不是整个列表,那将是伟大的!我试着使用len(),但我很难弄清楚如何在os walk搜索中使用它。 我想说最大的事情是
music=str(os.path.join(root,file))
,我相信这就是搜索

任何想法都将不胜感激。感谢您的时间,请尝试以下内容:

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

计数到5,然后从循环中
中断
。关于使用中断函数仅从函数返回5个字符串,是否有我可以使用的示例或我可以研究的链接?我在谷歌上搜索了一下,得到了一些关于break的信息,但是所有的例子都显示了数字,而我的函数没有数字。它只是目录路径中的一个文件字符串。就是这样!!我知道这与python使用的[]列表有关。(抱歉-noob.lol)我只是不知道在这种情况下如何使用它。(代码学院没用。哈哈)非常感谢!