问题描述
问题
我不久前发布了这个问题(此处: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,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,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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)