使用Python进行文件搜索

当你想搜索一个系统时,可能会有很多情况。假设在编写mp3播放器时,你可能想拥有所有的功能。mp3文件存在。下面是一个简单的方法。 这段代码搜索正在运行的文件中的所有文件夹。如果你想要其他类型的文件,只需更改扩展名。

null

Python3

# Python code to search .mp3 files in current
# folder (We can change file type/name and path
# according to the requirements.
import os
# This is to get the directory that the program
# is currently running in.
dir_path = os.path.dirname(os.path.realpath(__file__))
for root, dirs, files in os.walk(dir_path):
for file in files:
# change the extension from '.mp3' to
# the one of your choice.
if file .endswith( '.mp3' ):
print (root + '/' + str ( file ))


os不是python中的外部库。所以我觉得这是最简单也是最好的方法。 本文由 苏米特·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享