Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: 'str' object has no attribute 'write'

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

Someone please tell me why. Thx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading