Python 3.5 中的压缩,需要一个整数(得到类型 str)

0 python compression python-3.x

我试图制作一个程序,允许用户输入数据来指定文件名和扩展名,然后压缩该文件并允许他们命名它。但是我不断收到错误消息“需要一个整数(获取类型 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!")
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

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

with open(first_answer, ".", second_answer, 'rb') as in_file:
Run Code Online (Sandbox Code Playgroud)

函数的第三个位置参数open()是buffer参数,如果指定,该参数始终必须是整数。

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

with open(first_answer + "." + second_answer, 'rb') as in_file:
Run Code Online (Sandbox Code Playgroud)