Home > front end >  Error: 'str' object has no attribute 'write'
Error: 'str' object has no attribute 'write'

Time:02-17

Pycharm keeps giving this error in the function: 'str' object has no attribute 'write'

Someone please tell me why. Thx

...

    import tkinter as tk
    from tkinter import filedialog as fd

    def save_cust():
        s='Sample Text'
        file = fd.asksaveasfilename(initialdir="/test", title="Select file", defaultextension=".txt",
                filetypes=(("txt files", "*.txt"),("all files", "*.*")))

        file.write(s)
        file.close()

...

CodePudding user response:

Use this

import tkinter as tk
from tkinter import filedialog as fd

def save_cust():
    s='Sample Text'
    file = fd.asksaveasfilename(initialdir="/test", title="Select file", defaultextension=".txt",
    filetypes=(("txt files", "*.txt"),("all files", "*.*")))
    
    f = open(file,'w')
    f.write(s)
    f.close()

save_cust()

the file variable is path so open file and then write

  • Related