首页 > 编程语言 > 详细

python基础学习笔记——Python基础教程(第2版 修订版)第11章(文件与素材)

时间:2017-09-05 16:21:34      阅读:226      评论:0      收藏:0      [点我收藏+]

#文件模式 open(name[.mode[.buffering]])

r
w
a
b
+
f=open(rc:\text\somefile)

#基本文件方法

#对文件内容进行迭代

 

f=open(somefile.txt,w)  #r是默认的
f.write(hello)
f.read(4)
f.close()

#管式输出

$ cat somefile.txt|python somescript.py|sort

#读写行

writelines
readlines

#关闭文件

#对文件进行迭代

按字节处理
f=open(filename)
while True:
         char=f.read(1)
         if not char:break
         process(char)
f.close()

#按行操作
f=open(filename)
while True:
        line=f.readline()
        if not line:break
        process(line)

#读取所有内容
f=open(filename)
for char in f.read():
     process(char)

import fileinput
for line in fileinput.input(filenamr)
     process(line)
$迭代文件
for line inf:
     process(line)
file(name[.mode[.buffering]]) #打开一个文件并返回文件对象
open(name[.mode[.buffering]]) #file的别名

 

python基础学习笔记——Python基础教程(第2版 修订版)第11章(文件与素材)

原文:http://www.cnblogs.com/realmonkeykingsun/p/7479204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!