ここに私のコードがあります。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()
これは、それが
は、ファイルの名前を入力し、既存のもの
を使用するために、新しいファイルまたは2を作成するために、1を入力しています出力されます。
テスト
7を押してこのファイルにテキストを追加し、8を押してthの内容を切り捨てますファイルであるか、このファイルを削除するには9です:8
エラー!間違ったエントリ
は
にこの作業を行う方法についての任意のヘルプを既存のものを使用するように、新しいファイルまたは2を作成するために、1を入力してください?
あなたはあなたの条件文の前に '' int'にentry'を変換する必要があるに
entry
をキャスト。代わりに、あなたの条件文が 'if entry == '7':'のようなものだった場合、それも機能します。 – JCVanHammesysとargvは必要ありません。引数リストから実際にファイル名を読みたい場合は、 'filename = argv [1]'を使う必要があります。参照:https://docs.python.org/3.5/library/sys.html –
JCVanHammeに感謝しますが、それは所有者の場合には機能しますが、なぜそれがエントリのために働くことができないのですか? – ebi