请看下面的代码:我正在尝试从用户那里获取原始输入并打开文件并将其写入并从文件中读取并打印。在读取和打印文件内容时,我得到一些ASCII值或随机值,可能是\ n或输入我在输入文件中的内容后导致问题。不确定,请在这里帮忙。 谢谢, Nithin MP
import os
import codecs
from sys import argv
script, filename = argv
try:
txt = open(filename)
print "Here's your file %r:" % filename
print "content:\n",txt.read()
a=raw_input("For erasing file enter E or press A to append or press enter to exit :")
if a=="E":
txt = open(filename,"r+")
txt.truncate()
print "file content erased"
exit(1)
elif a=="A":
b=raw_input("enter text here :")
#txt = codecs.open(filename,"a+","utf-8")
txt = open(filename,"a+")
txt.write(b)
#print "new file content is :",txt.encode("utf-8")
print "new file content is :",txt.read()
exit(0)
else :
print "file not modified"
exit(0)
except Exception as e :
print e
#exit(0)