0

How would I be able to create a '.txt' file and store some information from an entry box and be able to update the '.txt' file?

I understand that I would have to use:

file = filedialog.asksaveasfile( mode = 'w', defaultextension = '.txt')

and store the information form the Entrybox into the file:

#the self.nameEntry had the input of 'zack'
name = self.nameEntry.get()
file.write(name)
file.close()

But when the program continues to run and I want to save the new information into the same .txt file, how would I be able to accomplish that without using filedialog.asksaveasfile() all over again? Would I use file = open(file) and then use the file.write()?

cahuizar
  • 35
  • 7
  • This question seems very familiar. I don't think you can do `open(file)`, because `file` isn't a file name. Have you tried using `asksaveasfilename` instead? – Kevin Dec 08 '14 at 19:43
  • if I use `asksaveasfilename` it wouldnt let me use the `mode = 'w'`. How would I be able to store the `name` into the file withot `file.write(name)`? – cahuizar Dec 08 '14 at 19:46
  • You can't specify a file mode for `asksaveasfilename`, because unlike `asksaveasfile`, it doesn't actually create the file object for you. You specify the mode in the call to `open` instead. – Kevin Dec 08 '14 at 19:48
  • "You specify the mode in the call to open instead." Could you possibly show me how it would look like with the actual code? Because I do not understand how to use the `open`. Thank you – cahuizar Dec 08 '14 at 19:53
  • Thanks for the tip.... As you can probably tell Im a litttl enew to this – cahuizar Dec 08 '14 at 21:08

1 Answers1

0

I was able to understand how to do it...

Code:

file_name = filedialog.asksaveasfilename(defaultextension = '.txt')
if file_name is None:
    return
file = open(file_name, mode 'w')
name = self.nameEntry.get()
file.write(name)
file.close()

Thanks a lot to Kevin for helping me out with my problem :)

cahuizar
  • 35
  • 7