1

My tkinker Python script is not working, it says the error ""tuple" object has no attribute "read"", how could I fix this?

My code is here: https://pastebin.com/kLnmutcg

My theory why it says this error is in this code:

def save():
    filename = filedialog.askopenfilename(filetypes=(("txt files","*.txt"),("All files","*.*")))
    file = open=(filename, "wt")

def open():
    filename = filedialog.askopenfilename(filetypes=(("txt files","*.txt"),("All files","*.*")))
    file = open=(filename, "rt")
    read = file.read()
    text_box.insert(tk.END, read)

I'm trying to make a notepad clone of sorts.

1
  • If you get this error it is often because your variable has been defined incorrectly or was inadvertently redefined, so that is always worth checking. Commented Feb 16, 2022 at 21:34

1 Answer 1

3

The correct syntax is:

file = open(filename, "wt")

By using file = open=(filename, "wt") you create two variables: file and open, that both contain a tuple (filename, "wt")

Also do not use open as variable/function name, this is a python builtin. You can find a list of python builtins in the documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

Now this spits out an error called "open() takes 0 positional arguments but 2 were given"
Make sure to restart your interpreter. If you messed with variable names you might have overwritten open with something else
Huh, I am using VS and restarted it multiple times during this
@Mikus You defined a function called open(). Don't define things with the same name as builtins. Call your function something else, like open_file()

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.