我TypeError: not all arguments converted during string formatting
在尝试打开文件时进入python 3.4.3。我正在使用以下两个模块:
def write(file, text):
file = open("%s.txt", "w" % (file))
file.write(text)
file.close()
和
import rnumb
import file
def create():
name = input("What is your name? ")
attack = rnumb.randn(1,3)
defense = rnumb.randn(1,3)
agility = rnumb.randn(1,3)
file.write("name",name)
file.write("attack",attack)
file.write("defense",defense)
file.write("agility",agility)
错误在 file = open("%s.txt", "w" % (file))
您的订单有误:
file = open("%s.txt" % file,"w")
您可能会发现str.format不太容易出错,也可以with
用来打开文件,因为它将自动为您关闭文件:
with open("{}.txt".format(file),"w") as f:
f.write(text)
下一个问题是尝试导入文件对象,如果必须在单独的模块中将其导入,请导入该write
函数。我只需在create中打开文件,然后再次使用str.format编写:
def create():
with open("{}.txt".format(file),"w") as f:
name = input("What is your name? ")
attack = rnumb.randn(1,3)
defense = rnumb.randn(1,3)
agility = rnumb.randn(1,3)
f.write("name {}".format(name))
f.write("attack {}".format(attack))
f.write("defense {}".format(defense))
f.write("agility {}".format(agility))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句