2015-06-02 28 views
0
#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() 

在promp在文件中寫入某些內容後,腳本會出錯,併產生奇怪的符號而不是輸入的文本。請幫助錯誤:在python輸出中獲取符號

+2

對不起,沒有精神。包括輸入和輸出。 –

回答

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() 
+1

我傻的。我仍然是一個新手,所以我一直在犯這些愚蠢的錯誤。它現在像一種魅力 –