2016-04-28 37 views
1

这里是我的代码,我可以使用你的帮助。阅读,写作,追加和删除python文件

我的程序应该接受数据输入,创建新的文本文件,读写文本文件,将文本追加到已有的文本文件中,截断和删除文件。 现在我的程序遇到的问题是应该附加文本,截断文本文件的内容并删除文本文件的代码部分不起作用,程序在运行时不会返回任何错误。

import os 
from sys import argv 

filename = argv 

def menu(): 
    holder = input("enter 1 to create new file or 2 to use existing ones\n") 
    if holder == 1: 
     dam = raw_input("Enter name of new text file:\n")+'.txt' 
     textfile = open(dam, 'w+') 
     happ = raw_input("\nPlease enter record into your new file\n\n") 
     textfile.write(happ) 
     textfile.close() 
     print "*******************" 
     print "*******************\n" 

     print "To view the content of your new file, enter 'yes' otherwise enter 'no' to exit" 

     gett = raw_input() 

     if gett == 'yes': 

      print "*******************" 
      print "\nyour inputted record is>>>\n" 
      display = open(dam) 
      print(display.read()) 
      print'\n' 
      menu() 
     elif gett == 'no': 
      print ("\nOk, see you later. Have a nice day!") 
      print'\n' 
      menu() 
     else: 
      print "\nyou have entered a wrong input" 
      print '\n' 
      menu() 
    elif holder == 2: 
     filename = raw_input("Enter name of file:\n")+'.txt' 
     entry = raw_input("Press 7 to append text into this file, 8 to truncate the content of this file, or 9 to delete this file : ") 
     if entry == 7: 
      print ("Displayed below is the content of your file, continue to append more text at the bottom of the file.(text is limited to 3 lines)\n") 
      textfiles = open(filename, 'a+') 
      print (textfiles.read()) 
      line1 = raw_input() 
      line2 = raw_input() 
      line3 = raw_input() 
      print "\nSaving..." 
      textfiles.write(line1) 
      textfiles.write('\n') 
      textfiles.write(line2) 
      textfiles.write('\n') 
      textfiles.write(line3) 
      textfiles.write('\n') 
      print "\nSaved!" 
      textfiles.close() 
     elif entry == 8: 
      textfiles = open(filename, 'w') 
      print "Truncating the file..." 
      textfiles.truncate() 
      print "Done, truncated." 
      textfiles.close() 
      right = raw_input("Do you want to write into this file? Y/N : ") 
      if right == 'Y': 
       textfiles = open(filename, 'a+') 
       print "text is limited to 3 lines" 
       line1 = raw_input('\n') 
       line2 = raw_input() 
       line3 = raw_input() 
       print "\nSaving..." 
       textfiles.write(line1) 
       textfiles.write('\n') 
       textfiles.write(line2) 
       textfiles.write('\n') 
       textfiles.write(line3) 
       textfiles.write('\n') 
       print "\nSaved!" 
       textfiles.close() 
      else: 
       print "Ok have a nice day" 
     elif entry == 9: 
      print "Deleting the file..." 
      try: 
       os.remove(filename) 
      except OSError, e: #if failed, report it back to the user 
       print ("Error: %s - %s." % (e.filename, e.strerror)) 
       print "Done, deleted." 
     else: 
      print "Error! wrong entry" 
      print '\n' 
      menu() 
    else: 
     print "\nyou have entered a wrong input" 
     print '\n' 
     menu() 
menu() 

这是它给人的输出

输入1来创建新文件或使用; 2,现有的

输入文件名:

测试

按7将文本追加到此文件中,8将截断内容是文件,或9删除此文件:8

错误!输入错误

输入1以创建新的文件或2以使用现有的

任何帮助就如何使这项工作?

+1

你需要你的条件语句之前entry'转换'来一个'int',否则比较将永远不会通过。或者,如果你的条件是'if entry =='7':',那也可以。 – JCVanHamme

+0

不需要sys和argv。如果你真的想从参数列表中读取文件名,你需要使用:'filename = argv [1]'。请参阅:https://docs.python.org/3.5/library/sys.html –

+0

感谢JCVanHamme,但它在持有人的情况下工作,为什么然后不能进入? – ebi

回答

0

您正在使用的功能raw_input()了变量entry在该行

entry = raw_input("Press 7 to append text into this file, 8 to truncate the content of this file, or 9 to delete this file : ") 

指的的python2https://docs.python.org/2/library/functions.html#raw_input)的文件,该功能raw_input返回String

之后,您正在测试变量entryinteger的值。由于String永远不会等于int,因此测试不起作用。

entry = input("Press 7 to append text into this file, 8 to truncate the content of this file, or 9 to delete this file : ") 

:而在最后一个条件块代码秋天这是“错误输入”

要解决这个问题,你应该像你这样在你的代码的开头(https://docs.python.org/2/library/functions.html#input)使用input功能或投entryint

entry = int(raw_input("Press 7 to append text into this file, 8 to truncate the content of this file, or 9 to delete this file : "))