1

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:

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))

2 Answers 2

3

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:

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:

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, sidnt realize "w" would mess it up
@Lord_Zane55, no worries, I made a couple of other changes. If you want to use the file multiple times and not recreate it every time you call create then move it outside the function but it is a lot easier open it in the same module. You also need to cast ints to str when writing to a file oruse format. write also only takes a single argument.
Thanks, I am merging the two modules as you suggested. However I so not underatand the rest of what you mean (Im new to python and havent made anything that advanced yet
@Lord_Zane55. I mainly meant file.write("attack",attack) won't work for two reasons. One attack etc.. are ints and write only takes strings and two, you cannot pass two arguments to write so using format creates on string.
1

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.

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

To fix this, you could use:

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

Or, Python's string.format function:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.