我想将一个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)
编码后的字符串太短,无法代表完整的字符串,因此结果不理想。那么为什么会“不起作用”呢?