在Python 3.5中的压缩,需要一个整数(GOT Type str)



im试图制作一个程序,该程序允许用户输入数据指定文件名和扩展名,然后压缩该文件并允许他们命名。但是,我一直收到错误消息"需要整数(GoT type str),它说它与'rb'点有关。有关如何解决此问题的任何信息?

import zlib
first_answer = input("Please input file name")
print(first_answer)
second_answer = input("Please input file extension")
with open(first_answer, ".", second_answer, 'rb') as in_file:
compressed = zlib.compress(in_file.read(), 9)
third_answer = input("What would you like to call this new file?")
with open(third_answer, "wb") as out_file:
out_file.write(compressed)
print("File has been compressed!")

您正在以几个单独的参数传递到 open()函数:

with open(first_answer, ".", second_answer, 'rb') as in_file:

open()函数的第三位置参数是buffer参数,如果指定了,它总是必须是整数。

您需要将这些字符串与+串联或使用字符串格式使其成为一个参数:

with open(first_answer + "." + second_answer, 'rb') as in_file:

最新更新