[英]Encode and decode a mp3 file
我想将mp3文件另存为文本文件中的编码字符串,但是它不适用于我的代码
import sys, base64
f = open(sys.argv[1], 'r')
b = base64.b64encode(f.read())
print sys.getsizeof(b)
f.close()
try:
file = open(sys.argv[2] + '.txt', 'w')
file.write(b)
file.close()
except:
print('Something went wrong!')
sys.exit(0)
f = open(sys.argv[2] + '.txt', 'r').read()
b = base64.b64decode(f)
f.close()
try:
file = open(sys.argv[2] + '2.mp3', 'w')
file.write(b)
file.close()
except:
print('Something went wrong!')
sys.exit(0)
编码的字符串太短,无法作为完整的字符串,因此效果不佳。 那么为什么“不”起作用呢?
好吧,我已经达到了我的个人目标。
正如五角大楼所提到的:
您需要使用“ rb”来调用open,因为它是二进制的。 使用len代替sys.getsizeof。
f = open(sys.argv[2] + '.txt', 'r').read()
b = base64.b64decode(f)
f.close()
我将其更改为
f = open(sys.argv[2] + '.txt', 'r')
b = base64.b64decode(f.read())
f.close()
因此,我对其进行了更改,当我最终再次创建mp3文件时,您需要编写二进制文件“ wb”,并且该文件可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.