【发布时间】:2020-11-23 06:33:21
【问题描述】:
我最近收到了一个 Python 脚本来解密 Excel 文件。我之前没有任何使用 Python 的经验,所以我一直在试图弄清楚如何通过 Google 搜索正确运行脚本,但我碰壁了。据我所知,该脚本已有几年历史了,对于 Python3 可能不是最新的。
from StringIO import StringIO
import os
import tkFileDialog
def crypt(t, k):
old = StringIO(t)
new = StringIO(t)
for position in xrange(len(t)):
bias = ord(k[position % len(k)])
old_char = ord(old.read(1))
new_char = chr(old_char ^ bias)
new.seek(position)
new.write(new_char)
new.seek(0)
return new.read()
dirname = tkFileDialog.askdirectory(initialdir="/", title='Please select a directory')
files = [f for f in os.listdir(dirname) if os.path.join(dirname, f)]
for f in files:
t = os.path.join(dirname, f)
tout = os.path.join(dirname, 'decr_%s' % f)
f_in = open(t, 'rb')
f_out = open(tout, 'wb')
key = "b8,xaA3rvXb-d&w8P6!9k7dQs.dbkLEw?t!3!`sM(,f!2^6h"
f_out.write(crypt(f_in.read(), key))
f_in.close()
f_out.close()
这是我第一次得到的脚本。在几个 ModuleNotFoundErrors 和 AttributeErrors 之后,我尝试进行更改。现在,出现的错误是:
Traceback (most recent call last):
File "/Users/xxx/Desktop/App/Decrypt.py", line 34, in <module>
f_out.write(crypt(f_in.read(), key))
File "/Users/xxx/Desktop/App/Decrypt.py", line 9, in crypt
old = StringIO(t)
TypeError: initial_value must be str or None, not bytes
不确定如何处理此错误 - 非常感谢任何帮助或建议!
【问题讨论】:
标签: python python-3.x list