Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I'm trying to create a file .txt and save the information that the user gives and also open the file in python i'm having trouble in create 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 ** it's where i put the file name Thank you very much in advance

like image 301
Sam Avatar asked Mar 23 '23 08:03

Sam


2 Answers

You can use the open built-in to get a file object with writing 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 linked docs for more info about the open arguments and other useful functions like writelines.

like image 93
Elias Dorneles Avatar answered Apr 25 '23 08:04

Elias Dorneles


Here's the code, I redesigned it to meet your requirements. Feedback would be much 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 data, if you clicked on listar you can see the output on the screen (though you can manually view the data which is saved in .txt file)

here's a sample:

Name : K-DawG Telephone No. : 911

The key here is using the with as statement, for more info check out Codeacademy's course on python

using a list and the insert() method was surely not the best option for this problem but rather if you use my method and write to a .csv file with delimiters the program could finally be worthwhile

like image 20
K DawG Avatar answered Apr 25 '23 08:04

K DawG