首页 > 解决方案 > TypeError: write() 参数必须是 str,而不是 int

问题描述

下面是我的代码

   for a in list(range(1,100)):
        print(a)
    with open("C:/Users/me/Downloads/Documents/lala",mode="w")as f:
        print(f.write(a))

错误是:

 TypeError: write() argument must be str, not int

标签: python

解决方案


您需要在编写之前将 a 转换为字符串

str(a)

您还应该删除 print 调用,因为您的文件操作中不应该有任何可打印的内容

f.write(str(a))

推荐阅读