Arlidio Dobra
Arlidio Dobra

Reputation: 61

Invalid file Save Error Python

I'm trying to write a script that allows a user to create a folder with any name they want, and then create a file with any name they want. Once they do they, the program asks them for 3 names and writes them into the file. I then want to allow the user to input a number from 1 to 3 and display the number of lines they want. I'm getting an error right now when trying to read the file saying something along the lines of

TypeError: invalid file: <_io.TextIOWrapper name='C:blah blah ' mode='a' encoding='cp1252'>

The code is below:

def SaveDir():
    global FileSave
    FileSave = filedialog.asksaveasfile(mode='a', defaultextension=".txt")
    if FileSave is None:                                                                                                                                          
        return
    print(FileSave)
    SaveDir2()

def SaveDir2():
    FinalFile = open(FileSave)
    FinalFile.write(PRINTCONV)
    FinalFile.close()

Upvotes: 2

Views: 616

Answers (1)

metatoaster
metatoaster

Reputation: 18908

The function tkFileDialog.asksaveasfile returns the actual open file, which is why you got a TypeEror as that is not a valid file name that can be opened. Consider using tkFileDialog.asksaveasfilename instead. Alternatively just simply call FileSave.write as that is the open file object.

Similar questions have been asked before:

Upvotes: 2

Related Questions