如何让python自动编写和保存文件

时间:2023-01-14 10:36:41

Is it possible to write a code in python that saves and writes files automatically?

是否可以在python中编写一个自动保存和写入文件的代码?

How I want the code to work: The user opens the program. The program asks for an input, lets say it asks for the users name. User inputs his name. The program then takes that name and writes it in an external file and saves. The user then closes the program. Next time the program is opened it imports the external file, thus knowing the name of the user.

我希望代码如何工作:用户打开程序。程序要求输入,让我们说它要求用户名。用户输入他的名字。然后程序获取该名称并将其写入外部文件并保存。然后用户关闭程序。下次打开程序时,它会导入外部文件,从而知道用户的名字。

1 个解决方案

#1


Yes of corse this are here a same introduction with some lines

是的corse这里有一些相同的介绍

import os


def write_to_file(name):
    text_file = open("%s.txt"% name , "w")
    text_file.write("%s" % name)
    text_file.close()

def check_exists(name):
    if os.path.exists("./"+name+".txt"):
        print "File found!"
    else:
        write_to_file(name)


def input_name():
    return(raw_input("input name: "))


check_exists(input_name())

#1


Yes of corse this are here a same introduction with some lines

是的corse这里有一些相同的介绍

import os


def write_to_file(name):
    text_file = open("%s.txt"% name , "w")
    text_file.write("%s" % name)
    text_file.close()

def check_exists(name):
    if os.path.exists("./"+name+".txt"):
        print "File found!"
    else:
        write_to_file(name)


def input_name():
    return(raw_input("input name: "))


check_exists(input_name())