#文件:内建函数open()返回一个文件对象,对该文件进行后续相关的操作需要大量用到。也有很多函数会返回文件对象或类文件对象(file-like)。文件时连续的字节,数据的传输经常会用到字节流。
#file的内建函数open() file()
#file()函数完全等同于open(),不过可以在处理文件对象是使用file(),如if instance(f,file)
file_name = "g:/a.txt"
#file_name是要打开的文件 access_mode是以什么方式打开r读 w写 a追加 w+ 可读可写。buffering 访问文件所采用的缓冲方式,0表示不缓冲,1表示只缓冲一行数据,任何大于1的值作为缓冲区大小,不填或为负,使用系统默认的缓冲机制
#file_object = open(file_name,access_mode = 'r',buffering = -1)
file_object = open(file_name, 'r', -1)
#通用换行支持符(UNS:universal NEWLINE Support),不同平台的行结束符号不一样,有些是\n \r \r\n,python可以通过UNS来处理这种情况。当每打开一个文本文件的一个新的一行,就会自动调用UNS,设置newlines,它的值为换行符,如果每行有不同的换行标示符,则newlines是一个元组
#文件的内建方法:当通过file() or open()返回一个对象后,后续对问价所有的操作都通过这个句柄来进行。分为输入,输出,文件
移动和杂项操作
#输入
#read():用来直接读取字节到字符串中,最多读取给定数目个字节,未给参数时或-1,直接读取到文件末尾
file_object.read()
#readline():方法返回文件的一行,包括行结束符。带参数同read(),如果size设置过大,会返回不完整的行
file_object.seek(0)
file_object.readline()
#readlines():读取所有文本文件剩余的行,作为一个字符串序列返回
file_object.seek(0)
file_object.readlines()
file_object.close()
#输出:write()把含有文本数据或二进制
数据库的字符串写入到文件中去,与read()和readline()相反
file_object = open(file_name, 'w', -1)
file_object.write("zhaoyehong")
context = ['zhaoyehong','dabendan']
file_object.writelines(context) #与readlines()相反,针对列表的操作,接受一个字符串列表作为参数,然后把他们写入文件,行结束符不会自动加入,需要的话,必须在调用writeline()前给每一行结尾上行结束符
file_object.close()
#文件移动seek(offset):offset代表相对某个位置的偏移量,默认值为0,代表从文件头算起,绝对偏移量 1代表从当前位置算起 ,2代表从文件末尾算起
#文件迭代:一行一行访问文件:
file_name = "g:/a.txt"
f = open(file_name, 'r', -1)
for eachLine in f:
print eachLine
f.close()
#其他:当一个文件被打开后,要关闭它,close()方法,python垃圾收集机制也会在文件对象的引用计数降至零的时候关闭文件
#调用flush()方法会直接把内存缓冲区中的数据立刻写入文件,而不是被动地等待输出缓冲区被写入。
#truncate(size = 23)方法:截取size字节处返回。如果没有参数,默认截取到文件的当前位置。
#文件方法杂项
file_name = "g:/b.txt"
f = open(file_name, 'r', -1)
for eachLine in f:
print eachLine ,
f.close()
#如何输出文件例子
import os
filename = raw_input("enter file name:")
fobj = open(filename,'w')
while True:
aLine = raw_input("enter a line('.'to quit):")
if aLine != '.':
fobj.write('%s%s'%(aLine,os.linesep))
else:
break
fobj.close()
#以可读可写的模式创建一个新文件,向文件中写入数据,然后使用seek()方法移动,使用tell()方法来展示移动的过程
f = open("g://a.txt",'w+')
print f.tell()
print f.tell()
f.write("test line2\n")
print f.tell()
f.seek(-12,1) #从第二行文件末尾后移动12个字节
print f.readline() #读取第二行全部字符
f.seek(0,0) #回到文件第一排第一个位置
print f.readline() #读取第一行
print f.tell() #到文件第一行末尾
print f.readline() #又读一行
print f.tell() #到第二行文件末尾
f.close ()
#文件内建属性
f = open("g://a.txt",'w+')
print f.name #返回文件的名字
print f.mode #文件打开的模式
print f.closed #判断文件是否关闭
print f.softspace #文件打印下一行前是否要加一个空格,0表示要加,1表示不加
print f.encoding #文件所使用的编码
print f.newlines #文件行的结尾符号,如果有多个,就会是一个tuple
#标准文件:stdin:标准输入 stdout:标准输出 stderr:标准错误,这三个标准文件是只要一运行程序,自动为你打开。通过sys来访问。通常print 是输入到stdout ,raw_input是输出到stdin.引用需要导入sys包
#命令行参数:argc是参数个数,argv是参数组成的字符串数组,argc就是表示argv的长度,而sys.argv[0]第一个数永远都是程序的名字
#文件系统:通过python的os模块来实现的。
#os 和os.path模块例子:创建了一个文本文件,输入了少量数据,然后重命名,输出文件内容。还进行了一些辅助性的文件操作。
import os
for tmpdir in ('g://a.txt','g://b.txt','g://c/txt'):
if os.path.isdir(tmpdir):
break
else:
print 'no temp directionary avaliable'
tmpdir = ''
if impdir:
os.chdir(tmpdir)
cwd = os.getcwd()
print '*** crrent temporary dictionary'
print cwd
print '*** creating example directionary'
os.mkdir('example')
os.chdir('example')
cwd = os.getcwd()
print '*** new working directionary:'
print cwd
print '*** original directionary listing:'
print os.listdir(cwd)