Home > Blockchain >  TypeError: write() argument must be str, not tuple | writelines(l)
TypeError: write() argument must be str, not tuple | writelines(l)

Time:07-13

I wanna make a program who edit the save of a game by only editing one specific line (line 6) on a .ini file but there is an error (TypeError: write() argument must be str, not tuple save.writelines(lines) )

from tkinter import *
from tkinter import filedialog
from pathlib import Path

#  All important variables here to simplify reading my code
directory = None
path = None
save = None
room = None


def main():
      print("\n[#] Select your game directory")
      tk = Tk()
      tk.geometry("0x0")
      tk.wm_attributes('-toolwindow', 'True')      
      global directory
      directory = filedialog.askdirectory(title="Select the game directory...")
      path = Path(f"{directory}/save_data_main.ini")
      if path.is_file():  # If save file exist
          save = open(path, 'r ')
          lines = save.readlines()
          save.close()
          print("\n[*] Your currently at the", lines[5])
          print("[!] Please choose a number superior than 0 and inferior than 1000!")
          room = input("[#] Change the room value : ")
          lines[5] = "room=", str(room)
          save = open(path, 'w ')
          save.writelines(lines)
          save.close()
          print("[*] Value changed with success.")
       else:  # Error save file doesnt exist
          print("\n[!] ERROR: Save file doesn't exist! Try to create a new Game.")
          input("[*] Press Enter to continue...")
          exit()

if __name__ == '__main__':
    main()

I didnt write all the code tho, i tried to make something more simple to read and with only the important parts!

CodePudding user response:

You should replace the line:

lines[5] = "room=", str(room)

With:

lines[5] = "room="   str(room)

In python the " " operator is used to concatenate string, with the "," you are creating a tuple.

lines[5] = "room=", str(room)

is the same that:

lines[5] = ("room=", str(room))

CodePudding user response:

In this line:

lines[5] = "room=", str(room)

you are making element 5 of your list a tuple with the string 'room=' and the string of the value room.

try this:

line[5] = "room="   str(room)

or do this:

lines[0] = f"room={str(room)}"
  • Related