2

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()
metatoaster
  • 17,419
  • 5
  • 55
  • 66
Arlidio Dobra
  • 61
  • 1
  • 9

1 Answers1

2

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:

Community
  • 1
  • 1
metatoaster
  • 17,419
  • 5
  • 55
  • 66