20161109 Python 读书笔记之文件和素材

来源:互联网 发布:淘宝客推广使用教程 编辑:程序博客网 时间:2024/05/21 21:59

文件和素材

#打开文件#open(name[.mode[,buffering]])#open函数使用一个文件名作为唯一的强制参数,然后返回一个文件对象。#譬如打开一个文件就可以这样f = open(r'C:\text\somefile.txt')#写操作f = open('somefile.txt','w')f.write('Hello,')f.write('World!')f.close()#读操作f = open('somefile.txt','r')print f.read(4)print f.read()

管式输出
就像LInux里面的shell一样,使用管道可以在一个命令后面续写其他的多个命令
管道符号将一个命令的标准输出和下一个命令的标准输出连在一起

import systext = sys.stdin.read()words = text.split()wordcount = len(words)print 'Wordcount:',wordcount#seek(offset[,whence]):这个方法把当前位置(进行读和写的位置)移动到offset定义的位置。#where.offset是一个字节数,whence默认是0,也就是说便宜量是从文件开始计算的。f = open(r'd:\PyCode\gh1.txt','w')f.write('01234567890123456789')f.seek(5)f.write('Hello,World!')f.close()f = open(r'd:\PyCode\gh1.txt')print f.read()f = open(r'd:\PyCode\gh1.txt')print f.read(3)print f.read(2)print f.tell()  #注意f.tell方法返回的数字在这种情况下是一个长整数,但不是所有的情况都是这样的

关闭文件

#如果想确保文件被关闭了,那么应该使用try/finally语句,并且在finally子句中调用close方法。open you file heretry:    #Write data to your filefinally:    file.close()#事实上,有专门为这种情况设计的语句,即with语句:with open("somefile.txt") as somefile:    do_something(somefile)#with语句可以打开文件并且将其值复制到变量上。之后就可以将数据写入语句体的文件。文件在语句结束后会被自动关闭,即使是由于一场硬气的结束也是如此#with语句只有在导入入此模块的情况下才可以使用from __future__ import with_statement

使用基本文件方法

#一个栗子   一个简单的文本文件  操作some.txt#让我们先试试已经知道的方法,首先是read()f = open(r'd:\PyCode\some.txt')print f.read(7)print f.read(4)f.close()#然后是read()f= open(r'd:\PyCode\some.txt')print f.read()f.close()#接着是readline():f = open(r'd:\PyCode\some.txt')for i in range(3):    print str(i) + ': ' + f.readline(),f.close()#以及readlines():import pprintpprint.pprint(open(r'd:\PyCode\some.txt').readlines())#注意本例中我们使用的是文件对象自动关闭的方式f = open(r'd:\PyCode\some.txt')f.write('this\nis no\nhaiku')f.close()#最后是wirtelines(list)f = open(r'd:\PyCode\some.txt')lines = f.readlines()f.close()lines[1] = "isn't a\n"f = open(r'd:\PyCode\some.txt','w')f.close()

对文件内容进行迭代

按字节处理

#一个栗子 用read方法对每个字符进行循环def process(string):print 'Processing: ',stringf = open(filename)char = f.read(1)while char:process(char)char = f.read(1)f.close()#用不同的个方式写循环f = open(filename)while True:char = f.read(1)if not char : breakprocess(char)f.close()#按行操作 在while循环中使用readlinef = open(filename)while True:line = f.readline()if not line: breakprocess(line)f.close()

文件迭代器 迭代文件

f = open(filename)for line in f:    process(line)f.close()f = open('somefile1.txt','w')f.write('First line\n')f.write('Second line\n')f.write('Third line\n')f.close()lines = list(open('somefile1.txt'))print linesfirst,second,third = open('somefile1.txt')print first + second + third

#小结

#类文件对象。类文件对象是支持read和readline方法(可能是write和writelines)的非正式对象
#打开和关闭文件。通过提供一个文件名,使用open函数打开一个文件。如果希望确保文件被正常关闭,即使发生错误时也是如此
#可以使用with语句。
#模式和文件类型。当打开一个文件时,也可以提供一个模式,比如'r'代表都模式,'w'代表写模式。还可以将文件作为二进制文件打开。
#标准流。3个标准的文件对象(在sys模块中的stdin、stdout和stderr)是一个类文件对象,该对象实现了UNIX标准的I/O机制。
#读和写。使用read或是write方法可以对文件对象或类文件样对象进行读写操作。
#读写行。使用readline和readlines和(用于有效迭代的)xreadlines方法可以从文件中读取行,使用writelines可以写入数据。
#迭代文件内容。有很多方法可以迭代文件内容。一般迭代文本中的行,通过迭代文件对象本身可以轻松完成,也有其他方法。

0 0
原创粉丝点击