Edvard Berger
Edvard Berger

Reputation: 23

How to write an integer to a file with Python

I'm trying to add saves to my code. The loading works, and I am able to write the strings into the text file, but I can't figure out how to write an integer (number) into the txt file.

I've tried to define the integers as strings but none have worked.

(Variable names are in Norwegian.)

def save():
    with open("a.txt") as f:
        f.write(int(penger))
        f.write(int(vognplass_list[0]))
        f.write(int(vognplass_list[1]))
        f.write(int(vognplass_list[2]))
        f.write(str(vognplasser[0]))
        f.write(str(vognplasser[1]))
        f.write(str(vognplasser[2]))

The error says:

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

Upvotes: 0

Views: 958

Answers (3)

Vishnu Balaji
Vishnu Balaji

Reputation: 215

python write supports only str writing so you need to explicitly convert int into str. Since python is open source maybe if you want to you can modify the source code for write function or build a user defined module/package as per your usage needs

besides when writing a txt file all of the input buffer is written as binary first so the concept of int or str file during writing is meaningless.

also add a 'w' write mode modifier in the open method here is my code

> def save():
>     with open("a.txt",'w') as f:
>         f.write(str(penger))
>         for i in range(0,len(vognplass_list)): 
>             f.write(str(vognplass_list[i]))
>         for i in range(0,len(vognplasser)):
>             f.write(str(vognplasser[i]))

Upvotes: 0

Samwise
Samwise

Reputation: 71454

As the error message says, f.write takes a str, not an int, so you need to convert your data to a string form in order to write it to the file. You then need to convert it back from str to int after loading it from the file.

Rather than reinventing the wheel, I'd suggest using the json module for this, which handles ints as well as nested lists:

import json

def save(penger, vognplass_list, vognplasser):
    with open("a.txt", "w") as f:
        json.dump([penger, vognplass_list, vognplasser], f)

You can then use json.load to read the data back, and it will be automatically converted into its original form (i.e. whatever type of object you passed as the argument to json.dump, in this case a list with an int and two more lists):

def load():
    # loads penger, vognplass_list, vognplasser from a.txt
    with open("a.txt") as f:
        return json.load(f)

and you can destructure that back into the original three variables:

penger, vognplass_list, vognplasser = load()

Upvotes: 1

Izaak Cornelis
Izaak Cornelis

Reputation: 322

The error message explains the problem:

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

While you're explicitly calling the int constructor:

f.write(int(penger))

Text files don't story any information about what is in the file, only the strings that are in the file. So a possible solution would be:

f.write(str(penger))

But do note that the type information of penger is lost when trying to read the text file later.

Upvotes: 1

Related Questions