Кодирование / декодирование Z64-файла B64 в текст и наоборот Python 3 - PullRequest
0 голосов
/ 25 марта 2019

У меня есть 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'
...