在python

时间:2016-04-28 12:12:39

标签: python ubuntu

这是我的代码,我可以使用你的帮助。

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

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以使用现有文件

2

输入档案名称:

测试

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

错误!错误输入

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

有关如何使这项工作的任何帮助?

1 个答案:

答案 0 :(得分:0)

您在

行使用变量raw_input()的{​​{1}}函数
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 : ") https://docs.python.org/2/library/functions.html#raw_input)的文档,函数python2返回raw_input

之后,您正在针对String值测试变量entry。由于integer永远不等于String,因此测试无效。并且您的代码属于最后一个条件块,即“错误输入”

要解决此问题,您应该使用int函数,就像在代码开头时所做的那样https://docs.python.org/2/library/functions.html#input):

input

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

int