【发布时间】: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