Python: how to create a .txt file and write information to it

I am trying to create a .txt file and save the information that the user gives as well as open the file in python I am having trouble creating the file

here is my code

from Tkinter import * raiz = Tk() frame = Frame(raiz) def cadastro(): form = Toplevel(raiz) Label(form, text='Nome: ').grid(column=0, row=0, sticky=E) Label(form, text='Celular: ').grid(column=0, row=1, sticky=E) nome = StringVar() celular = StringVar() a=Entry(form, textvariable=nome, width=15) a.grid(column=1, row=0, sticky=W) Entry(form, textvariable=celular, width=15).grid(column=1, row=1, sticky=W) def onCancel(): form.destroy() def onOk(): ******.insert('','end',text=nome.get(), values=celular.get()) onCancel() Button(form, text='OK', command=onOk).grid(column=0, row=2, sticky=E) Button(form, text='Cancel', command=onCancel).grid(column=1, row=2, sticky=W) def listar(): w = Button(raiz, text='Cadastrar',command=cadastro).grid() x = Button(raiz, text='Listar').grid() raiz.mainloop() 

the ** where I put the file name Thanks a lot in advance

+4
source share
3 answers

Here is the code, I reworked it to suit your requirements. Feedback will be highly appreciated

 from Tkinter import * raiz = Tk() frame = Frame(raiz) out = [] def cadastro(): form = Toplevel(raiz) Label(form, text='Nome: ').grid(column=0, row=0, sticky=E) Label(form, text='Celular: ').grid(column=0, row=1, sticky=E) nome = StringVar() celular = StringVar() a=Entry(form, textvariable=nome, width=15) a.grid(column=1, row=0, sticky=W) Entry(form, textvariable=celular, width=15).grid(column=1, row=1, sticky=W) def onCancel(): form.destroy() def onOk(): with open('outt.txt','w') as txt: txt.write('Name : ' + str(nome.get()) + ' ' + 'Telephone No. : ' + str(celular.get())) onCancel() Button(form, text='OK', command=onOk).grid(column=0, row=2, sticky=E) Button(form, text='Cancel', command=onCancel).grid(column=1, row=2, sticky=W) def listar(): with open('outt.txt','r') as txt_read: print txt_read.read() w = Button(raiz, text='Cadastrar',command=cadastro).grid() x = Button(raiz, text='Listar' , command=listar).grid() raiz.mainloop() 

after entering the data, if you clicked on listar, you can see the result on the screen (although you can manually view the data that is saved in the TXT file)

here is a sample:

Name: K-DawG Phone Number: 911

The key here is with as instruction, for more information check out the Codeacademy course in python

using a list, and the insert () method is certainly not the best option for this problem, but if you use my method and write a delimited CSV file, the program may finally be worth it

+2
source

You can use the open built-in to get the file object with write permissions, and then fill the content using the write function :

 file = open('<FILENAME>.txt', 'w') file.write('first line\n') file.write('second line\n') file.close() 

Check out the related docs for more information on open arguments and other useful features like writelines .

+3
source

I found a pretty simple way to abstract the concept of files with python's built-in I / O functions. I wrote about it here

If you want to write to a file, just do

 import sys sys.stdout = open ("Output.txt", "w") print "Name :", str(nome.get()), "Telephone No :", str(celular.get()) 

and if you want to read from a file just do

 import sys sys.stdin = open ("Input.txt", "r") print raw_input() 
+1
source

Source: https://habr.com/ru/post/1488919/


All Articles