Python 3.5中的压缩,需要一个整数(got type str)

Compression in Python 3.5, an integer is required (got type str)

我正在尝试制作一个程序,允许用户输入数据以指定文件名和扩展名,然后压缩该文件并允许他们为其命名。但是我不断收到错误消息“需要一个整数(得到类型 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: