重新梳理Python基础(4)
2013-03-16 12:10
441 查看
1. 文件写操作
文件写操作,需要在打开文件的时候添加参数,如
open("filename", "w")
以下是对文件操作的函数小结
参考代码
View Code
文件写操作,需要在打开文件的时候添加参数,如
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 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)
相关文章推荐
- 重新梳理Python基础(9)
- 重新梳理Python基础(7)
- 重新梳理Python基础(5)
- 重新梳理Python基础(10)
- 重新梳理Python基础(3)
- 重新梳理Python基础(8)
- 重新梳理Python基础(6)
- 重新梳理Python基础(1)
- 重新梳理Python基础(11)
- 重新梳理Python基础(2)
- Python基础知识梳理 - 第02部分
- 重新梳理一下Linux的基础指令
- Python基础知识梳理 - 前导部分
- Python基础知识梳理 - 第01部分
- Linux学习笔记重新梳理20180711 之 磁盘管理基础命令
- 借助幕课网的资源重新梳理下PHP基础知识,用以备忘。
- .net基础重新学习梳理(一)
- python 基础语法梳理(二)
- python3基础知识梳理
- python 基础语法梳理