简体   繁体   English

Python:如何创建文件.txt并在其中记录信息

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

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 我正在尝试创建文件.txt并保存用户提供的信息,并也在python中打开文件,但是我在创建文件时遇到了麻烦

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 * *是我放置文件名的地方,非常感谢

You can use the open built-in to get a file object with writing permissions, and then fill the content using the write function : 您可以使用内置open来获取具有写权限的文件对象,然后使用write函数填充内容:

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 . 请查看链接的文档,以获取有关打开参数和其他有用功能(如writelines)的更多信息。

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) 输入数据后,如果单击listar ,则可以在屏幕上看到输出(尽管您可以手动查看保存在.txt文件中的数据)

here's a sample: 这是一个示例:

Name : K-DawG Telephone No. : 911 姓名: K-DawG电话号码: 911

The key here is using the with as statement, for more info check out Codeacademy's course on python 此处的关键是使用with as语句,有关更多信息,请查看Codeacademy的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 使用列表和insert()方法肯定不是解决此问题的最佳选择,但是如果您使用我的方法并使用定界符写入.csv文件,则该程序最终可能值得

I found a pretty easy way to abstract the concept of files with python's builtin IO functions. 我发现了一种非常简单的方法来使用python的内置IO函数抽象文件的概念。 I blogged 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM