Python 我可以把列表传给PyGame音乐吗?

Python 我可以把列表传给PyGame音乐吗?,python,pygame,audio-player,Python,Pygame,Audio Player,Win 10/64位/3.5: 我想将变量(mp3Fin)中的.mp3列表传递给pygame.mixer.music.load()。有办法做到这一点吗?当我单独加载时,PyGame可以工作。我最近几周才开始学习编程/python,我是stackoverflow的新手,如果我遗漏了什么,我很抱歉 最后一次编辑这是最新的代码,应该是正确格式/缩进的。它仍然收到一个错误,但错误已更改为与“Rwops”相关的内容。在bing上只找到了一些关于RWOP的链接。将删除以前的代码,因为我认为它们不再有用 代码

Win 10/64位/3.5:

我想将变量(mp3Fin)中的.mp3列表传递给pygame.mixer.music.load()。有办法做到这一点吗?当我单独加载时,PyGame可以工作。我最近几周才开始学习编程/python,我是stackoverflow的新手,如果我遗漏了什么,我很抱歉

最后一次编辑这是最新的代码,应该是正确格式/缩进的。它仍然收到一个错误,但错误已更改为与“Rwops”相关的内容。在bing上只找到了一些关于RWOP的链接。将删除以前的代码,因为我认为它们不再有用

代码:

if catChow == cat1[0]:
        print("Welcome to SoCha's music player.")   
        def get_files(directories):
            mp3Pre = []
            for root, directories, files in os.walk(directories):
                for filename in files:
                #Joins the two strings in order to form the full filepath.
                    filepath = os.path.join(root, filename)
                    mp3Pre.append(filepath) #Adds to the mp3PreParse variable above.
            return mp3Pre
        mp3Post = get_files("C:\\Users\\SoCha\\Music")
        pygame.mixer.init()
        pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
        pygame.mixer.music.play()
Welcome to SoCha's music player.
Traceback (most recent call last):
  File "SoChaOS.2.0.py", line 38, in <module>
    pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
pygame.error: Couldn't read from RWops

***Repl Closed***
Rwops错误:

if catChow == cat1[0]:
        print("Welcome to SoCha's music player.")   
        def get_files(directories):
            mp3Pre = []
            for root, directories, files in os.walk(directories):
                for filename in files:
                #Joins the two strings in order to form the full filepath.
                    filepath = os.path.join(root, filename)
                    mp3Pre.append(filepath) #Adds to the mp3PreParse variable above.
            return mp3Pre
        mp3Post = get_files("C:\\Users\\SoCha\\Music")
        pygame.mixer.init()
        pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
        pygame.mixer.music.play()
Welcome to SoCha's music player.
Traceback (most recent call last):
  File "SoChaOS.2.0.py", line 38, in <module>
    pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])
pygame.error: Couldn't read from RWops

***Repl Closed***
欢迎使用SoCha的音乐播放器。
回溯(最近一次呼叫最后一次):
文件“SoChaOS.2.0.py”,第38行,在
pygame.mixer.music.load([s代表mp3Post中的s,如果s.endswith(“.mp3”)]))
pygame.error:无法从RWOP读取
***回复关闭***

这是我最后一次编辑这篇原创文章。

mp3fin
是一个字符串。你为什么要索引它?当您调用
split()
时,它返回一个数组,您可以打印该数组,然后将其丢弃

在最后一行
pygame.mixer.music.load(mp3Fin[1])
中,您正在拼接字符串并使用第二个字符,即“:”

您可能只需要调用
pygame.mixer.music.load(mp3Fin)


我想将变量(mp3Fin)中的.mp3列表传递给 pygame.mixer.music.load()

mp3Fin
不是一个列表。mp3Post是在目录中找到的文件列表。for循环迭代时,每个文件都位于
mp3Fin
中。尝试:

for mp3Fin in mp3Post:
    if mp3Fin.endswith(".mp3"):
        pygame.mixer.init()
        pygame.mixer.music.load(mp3Fin)

您可以尝试使用以下方法将列表传递到
load
buy:

mp3Post = get_files("C:\\Users\\SoCha\\Music")
pygame.mixer.init()
pygame.mixer.music.load([s for s in mp3Post if s.endswith(".mp3")])

请记住,
mp3Fin.split()
不会就地修改
mp3Fin
-
mp3Fin
保留字符串

因此,当您到达第41行时,
mp3Fin[1]
将获取
mp3Fin
位置1处的字符。这就是错误中“:”的来源,
C:/…

尝试直接发送字符串

pygame.mixer.music.load(mp3Fin)

要小心那些功能

需要明确的是(因为我们这里没有完整的文件)
pygame.mixer.music.load(mp3Fin[1])
在程序的第41行,对吗?是的,是的。41也是最后一行。我按照你最初的建议使用了它,但它仍然无法播放(尽管它没有崩溃)。我更新了我原来的帖子,向大家展示。我只是用打印来检查信息是否在mp3Fin中。@SoCha不可否认,我现在没有测试环境-我只是在盯着它看。看起来字符串都是正确的,您确定
.load
工作正常吗?我不熟悉
音乐播放器
,但如果它应该包含文件路径,并且这些文件路径都存在于您的系统中,那么这似乎是解决方案。“在最后一行中,pygame.mixer.music.load…这是一个…”我现在看到了。我只是用指纹检查一下,但我把自己弄糊涂了。尽管如此,我还是会尝试完全按照你的建议重写它,看看这是否有帮助,如果我犯了一个错误,我可能犯了其他错误。bbiaf.我没有正确地排列我的标识(它们本应作为“if catChow”的一部分嵌套。我排列了它们,现在我得到了一个正确的错误。“欢迎使用SoCha的音乐播放器。回溯(最近一次调用):pygame.mixer.music.load中第38行的文件“SoChaOS.2.0.py”([s代表mp3Post if s.endswith(“.mp3”)]))pygame.error:无法读取RWops的内容。RWops对我来说是新的,Bing只返回了3个结果,其中1个是这样。格式错误。此网站讨厌me@SoCha我阅读了
pygame.mixer.music.load
的文档。它只需要一个文件名或一个文件对象。你不能像我在post.Ar的最后一部分中显示的那样向它传递一个列表你想把多首曲目混合在一起吗?是的。我看到了同一个文档,但我只是假设file object代表variable/list,然后就尝试了smh。
这一点都不重要。我只是想做我自己的初学者项目。我试过其他几个mp3模块,但都不起作用。我会像其他人一样一个接一个地将它们添加到我的代码中我已经做了,除非你有更好的建议。如果你有建议,我只希望它在控制台中播放,仅此而已。不管怎样,谢谢你的帮助,@Swoogan@SoCha我不再理解你想要完成什么。我以为我解决了你最初提交的错误。