【问题标题】:Compression in Python 3.5, an integer is required (got type str)Python 3.5 中的压缩,需要一个整数(获取类型 str)
【发布时间】:2015-05-04 12:03:49
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x compression


    【解决方案1】:

    您将几个单独的参数传递给open() 函数:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多