如何解决如何仅打印/列出 OS.Walk 中的 5 个条目? 问题目标:研究
问题
我不久前发布了这个问题(此处: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
解决方法
它完全按照你的要求做。它向您显示每个目录中的 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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。