【问题标题】:writing a text file on python在 python 上写一个文本文件
【发布时间】:2016-11-07 20:11:13
【问题描述】:
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 export 中使用什么代码来替换 print(),以便将公式中打印的数据写入文本文件。 在写入数据之前,应要求用户输入文件名,并且代码应检查是否存在同名文件,如果存在,则询问用户是否应覆盖该文件。如果用户选择不覆盖文件,则应将其返回到输入文件名的部分。

【问题讨论】:

  • Google“python 检查文件是否存在”和“python 写入文件”
  • 在问这样一个基本问题之前,您应该阅读文档。互联网上有数以千计(如果不是数百万)的示例。
  • @BryanOakley 我确实检查了,但没有任何符合我的需求,我会再次检查,如果我发现有问题删除这篇文章。

标签: python file python-3.x


【解决方案1】:

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

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

【讨论】:

  • 那是我的副本 :(
【解决方案2】:

这是如何打印到 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()

【讨论】:

  • 直接将返回值与布尔值进行比较从来都不是一个好主意。一个更好的主意是简单地使用布尔值的真实性。例如。 if boolean_return_value:.
  • 我将从您的代码中举一个例子。将此if os.path.isfile(filename) == True: 更改为此if os.path.isfile(filename):。或者另一个例子,把这个elif os.path.isfile(filename) == False:改成这个elif not os.path.isfile(filename):
  • 所以不需要== True== False 因为没有== True 是一回事,你只需在末尾添加not 而不是== False? (如在 elif 之后直接添加 not)
  • 我已经编辑了答案,这样更好吗?现在是不是更 Pythonic 和干净了?
  • 我看不出发布与问题中不同的程序的意义。
猜你喜欢
  • 2021-07-03
  • 2016-01-31
  • 2020-10-20
  • 2018-06-07
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
相关资源
最近更新 更多