简体   繁体   English

如何修复此使用txt文件读取和写入文件的python程序

[英]How to fix this python program which uses txt files to read and write from

I wrote this code which is supposed to input, output and clear a file, but whenever I write to it twice in a row, the second input overrides the first does anyone know how to help? 我编写了应该输入,输出和清除文件的代码,但是每当我连续两次写入文件时,第二个输入就会覆盖第一个输入,有人知道如何提供帮助吗?

while True:
    inorout=input("Would you like to input, output, quit or clear history?")
    if inorout.lower() == "input":
      repairs = open('repairs.txt', 'w')
      customer = input('Customer: ')
      job = input('Service: ')
      date = input("Date(dd.mm.yyyy):")

      if customer and job and date:
        repairs.write('%s, %s, %s\n' %(customer, job, date))
      else:
        print("Not applicable")

That's because you have opened the file to write in w (write) mode, which will start writing from start every time. 这是因为您已打开文件以w (写入)模式写入文件,该模式每次都会从头开始写入。 You need to open the file in a+ (append mode). 您需要以a+ (附加模式)打开文件。

Replace the line (line no: 4) 更换管线(管线编号:4)

repairs = open('repairs.txt', 'w')

With: 附:

repairs = open('repairs.txt', 'a+')

For your help, this is your full working code. 为了您的帮助,这是您的完整工作代码。

while True:
    inorout=input("Would you like to input, output, quit or clear history?")
    if inorout.lower() == "input":
      repairs = open('repairs.txt', 'a+') # <----- this line
      customer = input('Customer: ')
      job = input('Service: ')
      date = input("Date(dd.mm.yyyy):")


      if customer and job and date:
        repairs.write('%s, %s, %s\n' %(customer, job, date))
      else:
        print("Not applicable")
      repairs.close()    # <------- this line 

    elif inorout.lower() == "output":
      repairs = open('repairs.txt', 'r')
      selected=input("What customer do you select?")
      output=repairs.readlines()
      stripped_output = []
      for line in output:
        stripped_output.append(line.strip())

      for pair in stripped_output:
        if pair.split(', ')[0] == selected:
          print(pair)
        else:
          print("Customer not found.")
      repairs.close()    # <------- this line 

    elif inorout.lower() == 'quit':
      break
    elif inorout.lower() == "clear history":
      open("repairs.txt", 'w').close()
      print("Databases successfully reset")
    else:
      print('Command not found.')

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

相关问题 如何用 python 从右到左读取的那些语言读取 txt 文件 - How to read txt files with those languages which is read from right to left with python 如何依次读取两个txt文件并将其写入python中的新文件? - how to sequentially read two txt files and write into a new file in python? 如何从需要在单独文件夹中读写 .txt 文件的 .py 文件创建 python 可执行文件 - How to create python executable from a .py file that need to read and write .txt files in a separate folder 如何在 Python 3 中写入 .txt 文件 - How to write to .txt files in Python 3 如何运行使用 Bash 中的 lambda 的 Python3 程序 - How to run Python3 program which uses lambda from the Bash 制作一个可以读写文本文件的程序 - Making a program which will read and write to text files 你如何并行化一个程序来在 python 中读写大文件? - How do you parallelize a program to read and write large files in python? Python如何从文件读取和写入列表 - Python how to read and write lists from files 将使用本地文件的 python 程序转换为 .exe - Converting a python program which uses local files to .exe 用python编写代码以从“ txt”文件中读取一些示例输入的最佳方法是什么? - What is the best way to write a code in python to read some sample input from a “txt” files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM