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.
参考代码
from sys import argv script, filename = argv print "We are going to erase %r." % filename print "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()
输出
E:\SkyDrive\python\the hard way to learn python>python ex16.py ex16_sample.txt We are going to erase 'ex16_sample.txt'. If you don't want that, hit CTRL-C If 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. 把一个文件写到另一个文件
from sys import argv from os.path import exists script, from_file, to_file = argv print "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. 贴个无聊的代码吧
from sys import argv from os.path import exists script, input_file = argv print "Is %r exsit?" % input_file exists(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 = 1 print_a_line(current_line, current_file) # current_line = current_line + 1 current_line += 1 print_a_line(current_line, current_file) current_line = current_line + 1 print_a_line(current_line, current_file)