python TypeError:在读取和写入文件时,并非在字符串格式化期间转换所有参数

时间:2023-01-14 14:51:59

I am getting TypeError: not all arguments converted during string formatting in python 3.4.3 while trying to open a file. I am using the following two modules that I made:

我得到TypeError:在尝试打开文件时,并不是在python 3.4.3中的字符串格式化期间转换的所有参数。我正在使用以下两个模块:

def write(file, text):
    file = open("%s.txt", "w" % (file))
    file.write(text)
    file.close()

And

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)

The error is at file = open("%s.txt", "w" % (file))

错误发生在file = open(“%s.txt”,“w”%(file))

2 个解决方案

#1


You have the order wrong:

你订单错了:

file = open("%s.txt" % file,"w")

You might find str.format less error prone, also use with to open your files as it will automatically close them for you:

您可能会发现str.format不易出错,也可以用来打开文件,因为它会自动关闭它们:

with open("{}.txt".format(file),"w") as f:
     f.write(text)

You next problem is trying to import the file object, if you must have it in a separate module import the write function. I would simply open the file in create and again use str.format to 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))

#2


This is actually an issue with the method you provided for writing to a file.

这实际上是您提供的用于写入文件的方法的问题。

In this line of code:

在这行代码中:

file = open("%s.txt", "w" % (file))

You use % (file) to format the filename, %s.txt, but you provided the "w" - the second argument of the function - before you formatted the first one.

您使用%(文件)格式化文件名%s.txt,但在格式化第一个参数之前,您提供了“w” - 函数的第二个参数。

So, it's trying to run "w" % fileso it tries to fit the file into the "w" instead of the formatting string.

所以,它试图运行“w”%file,它试图将文件放入“w”而不是格式化字符串。

To fix this, you could use:

要解决此问题,您可以使用:

file = open("%s.txt" % (file), "w")

Or, Python's string.format function:

或者,Python的string.format函数:

file = open("{}.txt".format(file), "w")

The difference here is that the format is occuring on the first argument instead of the second, so the argument can find the %s or {} and fit itself in.

这里的区别在于格式是在第一个参数而不是第二个参数上发生的,因此参数可以找到%s或{}并且适合自己。

#1


You have the order wrong:

你订单错了:

file = open("%s.txt" % file,"w")

You might find str.format less error prone, also use with to open your files as it will automatically close them for you:

您可能会发现str.format不易出错,也可以用来打开文件,因为它会自动关闭它们:

with open("{}.txt".format(file),"w") as f:
     f.write(text)

You next problem is trying to import the file object, if you must have it in a separate module import the write function. I would simply open the file in create and again use str.format to 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))

#2


This is actually an issue with the method you provided for writing to a file.

这实际上是您提供的用于写入文件的方法的问题。

In this line of code:

在这行代码中:

file = open("%s.txt", "w" % (file))

You use % (file) to format the filename, %s.txt, but you provided the "w" - the second argument of the function - before you formatted the first one.

您使用%(文件)格式化文件名%s.txt,但在格式化第一个参数之前,您提供了“w” - 函数的第二个参数。

So, it's trying to run "w" % fileso it tries to fit the file into the "w" instead of the formatting string.

所以,它试图运行“w”%file,它试图将文件放入“w”而不是格式化字符串。

To fix this, you could use:

要解决此问题,您可以使用:

file = open("%s.txt" % (file), "w")

Or, Python's string.format function:

或者,Python的string.format函数:

file = open("{}.txt".format(file), "w")

The difference here is that the format is occuring on the first argument instead of the second, so the argument can find the %s or {} and fit itself in.

这里的区别在于格式是在第一个参数而不是第二个参数上发生的,因此参数可以找到%s或{}并且适合自己。