Python模块File简介
Python提供了File模块进行文件的操作,他是Python的内置模块。我们在使用File模块的时候,必须先用Popen()函数打开一个文件,在使用结束需要close关闭文件。
常用函数
序号
方法
描述
1
file.close()
关闭文件。关闭后文件不能再进行读写操作。
2
file.flush()
刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。
3
file.fileno()
返回一个整型的文件描述符(file descriptor FD 整型), 可以用在如os模块的read方法等一些底层操作上。
4
file.isatty()
如果文件连接到一个终端设备返回 True,否则返回 False。
5
file.next()
返回文件下一行。
6
file.read([size])
从文件读取指定的字节数,如果未给定或为负则读取所有。
7
file.readline([size])
读取整行,包括 “\n” 字符。
8
file.readlines([sizehint])
读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比sizhint较大, 因为需要填充缓冲区。
9
file.seek(offset[, whence])
设置文件当前位置
10
file.tell()
返回文件当前位置。
11
file.truncate([size])
截取文件,截取的字节通过size指定,默认为当前文件位置。
12
file.write(str)
将字符串写入文件,没有返回值。
13
file.writelines(sequence)
向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。
File模块使用实例
1.打开和关闭文件
open()
close()
有两种内建函数可以获取文件对象:open和file。他们的用法完全一样。下面只以open()为例子讲解。获取一个文件对象(打开文件)的语法如下:
Python
fileObj = open(filename,access_mode='r',buffering=-1)
fileObj=open(filename,access_mode='r',buffering=-1)
filename不用说你也应该知道是你要打开文件的路径。
access_mode用来标识文件打开的模式,默认为r(只读)。
常用的模式如下表所示:
文件模式
解释
r
以只读方式打开
w
以写方式打开,文件不为空时清空文件;文件不存在时新建文件。
a
追加模式,没有则创建
r+,w+,a+
以读写模式打开,参见w,a 另外还有一个b表示二进制模式访问,但是对于Linux或者unix系统来说这个模式没有任何意义,因为他们把所有文件都看作二进制文件,包括文本文件。
第三个参数不经常用到,标识访问文件的缓冲方式,0代表不缓冲,1代表缓
冲一行,-1代表使用系统默认缓冲方式。只要使用系统默认就好。
一些例子:
Python
>>> f = open('/etc/passwd','r')
>>> f1 = open('/etc/test','w')
>>>f=open('/etc/passwd','r')
>>>f1=open('/etc/test','w')
使用完文件后,一定要记得关闭文件,操作如下:
Python
>>> f.close()
>>>f.close()
2.文件读取
file.read(size = -1)
读取从当前文件游标起size个字节的文件内容。如果size=-1,则读取所有剩余字节。
Python
>>> f = open('/etc/passwd')
>>> f.read(100)
'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nol'
>>>f=open('/etc/passwd')
>>>f.read(100)
'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nol'
file.readlines(size=-1)
从文件中读取并返回一行(包括行结束符),或返回最大size个字符
Python
>>> f.readline()
>>>f.readline()
‘ogin\n’#和上面一个例子输出的最后拼起来就是 ‘nologin’,因为游标在l后面。
Python
>>> f.readline(1)
'a'
>>>f.readline(1)
'a'
file.readlines(sizhint=0)
读取文件所有的行,并作为一个列表返回(包括行结束符),如果sizhint>0则返回总和大约sizhint字节的行(具体由缓冲区大小决定)。
Python
f.readlines()
['dm:x:3:4:adm:/var/adm:/sbin/nologin\n', 'lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n', 'sync:x:5:0:sync:/sbin:/bin/sync\n', 'shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n', 'halt:x:7:0:halt:/sbin:/sbin/halt\n', 'mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n', ......
f.readlines()
['dm:x:3:4:adm:/var/adm:/sbin/nologin\n','lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n','sync:x:5:0:sync:/sbin:/bin/sync\n','shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n','halt:x:7:0:halt:/sbin:/sbin/halt\n','mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n',......
输出省略。
3.文件写入
file.write(str)
向文件中写入指定的字符串。
Python
>>> f = file('/root/test.py','w+')
>>> f.write("print 'hello,world'")
>>> f.read()
''
>>> f.close()
>>> file('/root/test.py','r').read()
"print 'hello,world'"
>>>f=file('/root/test.py','w+')
>>>f.write("print 'hello,world'")
>>>f.read()
''
>>>f.close()
>>>file('/root/test.py','r').read()
"print 'hello,world'"
file.write(seq)
向文件写入字符串序列seq。seq是任何返回字符串的可迭代对象。
Python
>>> f = file('/root/test.py','a+')
>>> codelst = ['\n','import os\n',"os.popen('ls').read()\n"]
>>> f.writelines(codelst)
>>> f.close()
>>> file('/root/test.py','r').read()
"print 'hello,world'\nimport os\nos.popen('ls').read()\n"
>>>f=file('/root/test.py','a+')
>>>codelst=['\n','import os\n',"os.popen('ls').read()\n"]
>>>f.writelines(codelst)
>>>f.close()
>>>file('/root/test.py','r').read()
"print 'hello,world'\nimport os\nos.popen('ls').read()\n"
注意,文件写入的时候,不会自动加上换行符,必须手动加上。
4.文件移动
学过C语言的同学,一定对fseek()函数不陌生,在Python中,seek()方法是fseek()的替代者。
seek(offset,whence=0)
方法可以将文件游标移动到文件的任意位置。其中offset代表需要移动的偏移字节数,whence表示要从哪个位置开始偏移:
0代表从文件开头开始算起,
1代表从当前位置开始算起,
2代表从文件末尾算起。
那我们如何知道当前文件游标在哪里呢?别担心,这里有个tell()方法可以返回当前文件游标的位置。
Python
>>> f = file('test.py','r')
>>> f.tell() #文件开头
0
>>> f.seek(5)#从默认的文件头开始移动5个字节
>>> f.tell()
5
>>> f.seek(5,1)#从当前游标处移动5个字节
>>> f.tell()
10
>>> f.seek(1,2)
>>> f.tell()
52
>>>f=file('test.py','r')
>>>f.tell()#文件开头
0
>>>f.seek(5)#从默认的文件头开始移动5个字节
>>>f.tell()
5
>>>f.seek(5,1)#从当前游标处移动5个字节
>>>f.tell()
10
>>>f.seek(1,2)
>>>f.tell()
52
5.文件迭代
在Python中,文件不仅仅是一个对象,而且是一个可迭代对象!我们可以利用如下迭代方式,轻松的访问和处理文件内容,而不必全部读出(readlines)后再迭代(性能上差了很多哦!)
Python
>>> for eachline in file:
... print(eachline)
>>>foreachlineinfile:
...print(eachline)