【问题标题】:Error: Getting symbols in python output错误:在 python 输出中获取符号
【发布时间】:2015-06-02 17:13:49
【问题描述】:
#I wrote the following code for making a text editor.
print "This is a simple text editor"
from sys import argv
script, name = argv
print "You have selected the file : %s" %name
print "Opening the file...."
t = open(name, 'r+')
print "The contents of the file are" 
print t.read()
f = open(name, 'w+')
print "Now we will truncate the file and empty it of it's contents"
f.truncate()
print "Now let us write something into our file\n"
x = raw_input('What do you want to write\n') #Works fine till here
f.write(x)
print "Now we read our file again"
print f.read()
print "And finally we close the file"
f.close()

提示在文件中写入内容后,脚本出错并产生奇怪的符号而不是键入的文本。请帮忙

【问题讨论】:

  • 对不起,不是通灵。包括输入和输出。

标签: python python-2.7 text file-io editor


【解决方案1】:

您需要关闭并重新打开文件。

print "This is a simple text editor"
from sys import argv
script, name = argv
print "You have selected the file : %s" %name
print "Opening the file...."
t = open(name, 'r+')
print "The contents of the file are"
print t.read()
t.close()  ##########
f = open(name, 'w+')
print "Now we will truncate the file and empty it of it's contents"
f.truncate()
print "Now let us write something into our file\n"
x = raw_input('What do you want to write\n') #Works fine till here
f.write(x)
f.close()  ##########
f = open(name, 'r+')  ##########
print "Now we read our file again"
print f.read()
print "And finally we close the file"
f.close()

【讨论】:

  • 我真傻。我还是个新手,所以我一直在犯这些愚蠢的错误。它现在就像一个魅力
猜你喜欢
  • 2020-03-09
  • 2016-08-24
  • 2020-07-25
  • 2016-02-25
  • 1970-01-01
  • 2016-03-02
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多