我想将所有mp3文件写入某个目录中的文件中。
我用了这段代码
import os
path = 'P:\dn\test55'
wrname = r'P:\dn\path\test55.txt'
test_files = [f for f in os.listdir(path) if f.endswith('.mp3')]
f = open(wrname, "w")
f.write(str(test_files))
f.close()
该文件也被写入,但是看起来像这样
['001-file.mp3', '002-file.mp3', '003-file.mp3']
但我希望文件看起来像这样:
001-file.mp3
002-file.mp3
003-file.mp3
我该如何更改?
非常感谢
该write
方法将其输入字符串写入文件中。然后,您需要传递密码以在文件中写入所需的实际字符串:mp3名称\n
,用表示“转到行”的字符分隔
f.write("\n".join(test_files))
字符串的join方法将一个列表作为输入,然后将列表中的元素连接在一起,并用该字符串从中调用该方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句