将B64 zip文件编码/解码为文本,反之亦然Python 3

时间:2019-03-25 01:53:57

标签: python-3.x text encoding zip

我有一个zip文件,我想将其编码/写入一个文本文件。 接下来,我需要解码文本文件并将其写入zip文件。 我在以下代码中做错了什么? 我收到以下错误。

# Encoding zip to text file
import base64

f = open('xpdf.txt','wb')
x = open('xpdf-tools-win-4.01.01.zip','rb').read()
f.write(base64.b64encode(x))
f.write(b.encode('utf8'))
f.close()

以下是此步骤的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-108-a3559e490128> in <module>
      4 x = open('xpdf-tools-win-4.01.01.zip','rb').read()
      5 f.write(base64.b64encode(x))
----> 6 f.write(b.encode())
      7 f.close()

AttributeError: 'bytes' object has no attribute 'encode'
# Decoding text to zip
import base64

t = open('xpdf1.txt','rb').read()
f = open('xpdf-tools-win-4.01.zip','wb')
f.write(base64.b64decode(f))
f.close()

以下是此步骤的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-111-e47fc1159960> in <module>
      3 t = open('xpdf1.txt','rb').read()
      4 f = open('xpdf-tools-win-4.01.zip','wb')
----> 5 f.write(base64.b64decode(f))
      6 f.close()

~\Anaconda3\lib\base64.py in b64decode(s, altchars, validate)
     78     in the input result in a binascii.Error.
     79     """
---> 80     s = _bytes_from_decode_data(s)
     81     if altchars is not None:
     82         altchars = _bytes_from_decode_data(altchars)

~\Anaconda3\lib\base64.py in _bytes_from_decode_data(s)
     44     except TypeError:
     45         raise TypeError("argument should be a bytes-like object or ASCII "
---> 46                         "string, not %r" % s.__class__.__name__) from None
     47 
     48 

TypeError: argument should be a bytes-like object or ASCII string, not 'BufferedWriter'

0 个答案:

没有答案