1. 檔案寫操作
檔案寫操作,需要在開啟檔案的時候添加參數,如
open("filename", "w")
以下是對檔案操作的函數小結
close -- Closes the file. Like File->Save.. in your editor.read -- Reads the contents of the file, you can assign the result to a variable.readline -- Reads just one line of a text file.truncate -- Empties the file, watch out if you care about the file.需要open的時候添加w參數write(stuff) -- Writes stuff to the file.
參考代碼
View Code
from sys import argvscript, filename = argvprint "We are going to erase %r." % filenameprint "If you don't want that, hit CTRL-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 am 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()
輸出
View Code
E:\SkyDrive\python\the hard way to learn python>python ex16.py ex16_sample.txtWe are going to erase 'ex16_sample.txt'.If you don't want that, hit CTRL-CIf you do want that, hit RETURN?Opening the file...Truncating the file. Goodbye!Now I am going to ask you for three lines.line 1: I have some books.line 2: You have some books.line 3: He has some books.I'm going to write these to the file.And finally, we close it.
open模式的小結,參考http://hi.baidu.com/leejun_2005/item/3f6f8ddc8a8b3b56d73aae64
"r" 以讀方式開啟,只能讀檔案 , 如果檔案不存在,會發生異常 "w" 以寫方式開啟,只能寫檔案, 如果檔案不存在,建立該檔案,如果檔案已存在,先清空,再開啟檔案"rb" 以二進位讀方式開啟,只能讀檔案 , 如果檔案不存在,會發生異常 "wb" 以二進位寫方式開啟,只能寫檔案, 如果檔案不存在,建立該檔案,如果檔案已存在,先清空,再開啟檔案"rt" 以文本讀方式開啟,只能讀檔案 , 如果檔案不存在,會發生異常 "wt" 以文本寫方式開啟,只能寫檔案, 如果檔案不存在,建立該檔案,如果檔案已存在,先清空,再開啟檔案"rb+" 以二進位讀方式開啟,可以讀、寫檔案 , 如果檔案不存在,會發生異常 "wb+" 以二進位寫方式開啟,可以讀、寫檔案, 如果檔案不存在,建立該檔案,如果檔案已存在,先清空,再開啟檔案
2. 把一個檔案寫到另一個檔案
View Code
from sys import argvfrom os.path import existsscript, from_file, to_file = argvprint "Copying from %s to %s" % (from_file, to_file)in_file = open(from_file)fromdata = in_file.read()print "The input file is %d bytes long" % len(fromdata)print "Does the output file exist? %r" % exists(to_file) #如果不存在,會建立這個檔案print "Ready, hit RETURN to continue, CTRL-C to abort."raw_input()out_file = open(to_file, "w")out_file.write(fromdata)print "All done"out_file.close()in_file.close()
3. 貼個無聊的代碼吧
View Code
from sys import argvfrom os.path import existsscript, input_file = argvprint "Is %r exsit?" % input_fileexists(input_file)indata = """I have a book.You have two.He has three."""open(input_file, "w").write(indata)def print_all(f): print f.read()def rewind(f): f.seek(0)def print_a_line(line_count, f): print str(line_count) + ": ", f.readline()current_file = open(input_file)print "First let's print the whole file:\n"print_all(current_file)print "Now let's rewind, kind of like a tap."rewind(current_file)print "Let's print three lines"current_line = 1print_a_line(current_line, current_file)# current_line = current_line + 1current_line += 1print_a_line(current_line, current_file)current_line = current_line + 1print_a_line(current_line, current_file)