在python上写一个文本文件

时间:2016-11-07 20:11:13

标签: python file python-3.x

import time
def mainmenu ():
    print ("1.set values")
    print ("2. run formula")
    print ("3. export formula results")
    maininput = int(input("Enter: "))
    if maininput == 1:
        set_values ()
    elif maininput == 2:
        formula ()
    elif maininput == 3:
        export ()

def set_values ():
    set_values.first = int(input("Value 1 between 1 and 10"))
    while 1< set_values.first <10:
        set_values.second = int(input("Value 2 between 1 and 10"))
        while 1< set_values.second <10:
            mainmenu ()
    else:
        print ("That is not a valid value")
        return set_values ()

def formula ():
    part_1 = set_values.first + set_values.second
    print ("Value 1 + value 2 =",part_1)
    time.sleep(2)
    part_2 = part_1 * 5
    print ("Value 1 + value 2 x 5 =",part_2)
    time.sleep(2)
def export ():
    print ()

mainmenu ()

我将在def导出中使用什么代码来替换print(),以便将公式中打印的数据写入文本文件。 在写入数据之前,应要求用户输入文件名,代码应检查是否存在具有相同名称的文件,如果存在,则询问用户是否应覆盖该文件。如果用户选择不覆盖文件,则应将其返回到输入文件名的部分。

2 个答案:

答案 0 :(得分:1)

您应参阅openwrite的文档(链接here)。除此之外,写入文件的首选方法如下:

with open('myfile.txt', 'w') as f:
   f.write('Writing to files is easy')

答案 1 :(得分:0)

这是打印到txt文件的方法:

file = open("Exported.txt", "w")
file.write("Text to write to file")
file.close()

另一种方法是:

with open('Exported.txt', 'w') as file:
   file.write("Text to write to file")

这是我编写txt文件的程序:

import os.path

def start():

    print("What do you want to do?")
    print("    Type a to write a file")
    print("    Type b to read a file")
    choice = input("            -")
    if choice == "a":
        create()
    elif choice == "b":
        read()
    else:
        print("Incorrect spelling of a or b\n\n")
        start()


def create():

    print()
    filename = input("What do you want the file to be called?\n")
    if os.path.isfile(filename):
        print("This file already exists")
        print("Are you sure you would like to overwrite?")
        overwrite = input("y or n")
        if overwrite == "y":
            print("File has been overwritten")
            write(filename)
        else:
            print("I will restart the program for you")
    elif not os.path.isfile(filename):
        print("The file has not yet been created")
        write(filename)
    else:
        print("Error")





def write(filename):
    print()
    print("What would you like the word to end writing to be?")
    keyword = input()
    print("What would you like in your file?")
    text = ""
    filename = open(filename, 'w')
    while text != keyword:
        filename.write(text)
        filename.write("\n")
        text = input()


def read():
    print()
    print("You are now in the reading area")
    filename = input("Please enter your file name:     -")
    if os.path.isfile(filename):
        filename = open(filename, 'r')
        print(filename.read())
    elif not os.path.isfile(filename):
        print("The file does not exist\n\n")
        start()
    else:
        print("Error")


start()