python3播放本地mp3文件_如何使用python3从文件夹/目录中搜索和播放mp3文件?

我想写一个python代码,当给定一个字符串(要搜索的mp3文件的名称)时,它将搜索与给定字符串和文件名匹配的目录。一旦找到,它就会打开并播放。在

我有一个代码,它在给定的目录中搜索给定的字符串。是否可以修改此代码以实现上述任务?谢谢您。`在#Import os module

import os

# Ask the user to enter string to search

search_path = input("Enter directory path to search : ")

file_type = input("File Type : ")

search_str = input("Enter the search string : ")

# append a directory separator if not already present

if not (search_path.endswith("/") or search_path.endswith("\\") ):

search_path = search_path + "/"

# If path does not exist, set search path to current directory

if not os.path.exists(search_path):

search_path ="."

# Repeat for each file in the directory

for fname in os.listdir(path=search_path):

# Apply file type filter

if fname.endswith(file_type):

# Open file for reading

fo = open(search_path + fname)

# Read the first line from the file

line = fo.readline()

# Initialize counter for line number

line_no = 1

# Loop until EOF

while line != '' :

# Search for string in line

index = line.find(search_str)

if ( index != -1) :

print(fname, "[", line_no, ",", index, "] ", line, sep="")

# Read next line

line = fo.readline()

# Increment line counter

line_no += 1

# Close the files

fo.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值