下面的命令,是你應該記住的命令:
close – 關閉檔案。跟你編輯器的 檔案->儲存… 乙個意思。
read – 讀取檔案內容。你可以把結果賦給乙個變數。
readline – 讀取文字檔案中的一行。
truncate – 清空檔案,請謹慎使用該命令。
write(『stuff』) – 將stuff寫入文
書中**:
**修改為:from sys import ar**
script, filename = ar**
print "we're going to erase %r." % filename
print "if you don't want that, hit ctrl-c (^c)."
print "if you do want that, hit return."
raw_input("?")
print "opening the file..."
target = open(filename, 'w')
print "truncating the file. goodbye!"
target.truncate()
print "now i'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "i'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print "and finally, we close it."
target.close()
輸出結果:from sys import ar**
script, filename = ar**
print("we're going to erase % r. " % filename)
print("if you don't want that, hit ctrl-c(^c).")
print("if you do want that, hit return.")
input("?")
print("opening the file...")
target = open(filename, 'w')
print("truncating the file. goodbye!")
target.truncate()
print("now i'm going to ask you for three lines.")
line1 = input("line 1:")
line2 = input("line 2:")
line3 = input("line 3:")
print("i'm going to write these to the file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print("and finally, we close it.")
target.close()
開啟text.txt檔案檢查檔案中的內容
python系統學習日記 L17 更多檔案操作
書中 from sys import ar from os.path import exists script,from file,to file ar print copying from s to s from file,to file we could do these two on one ...
Python系統學習 02
資料型別 數值用於計算,布林值用於條件執行等等。例子 s i m a student 錯誤,也可以在 前面新增轉義字元 s i m a student s1 morgan s2 xu print s1 s2 morgan xu s1 morgan print s1 8 morgan morgan m...
Python系統學習 07
一種規範,寫 時的規範 from abc import abcmeta,abstractmethod class istream metaclass abcmeta abstractmethod defread self,data print read super method abstractme...