以下讲解python3中对文件操作的函数
file.close()
含义:close()方法用于关闭一个已打开的文件,关闭后的文件不能在进行读写操作。否则会触发ValueError错误。
语法:file.close();
返回值:无
以下实例演示close()用法:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#关闭文件
f.close()
#python3 test.py
The file name is: /root/dpc
如果关闭该文件时再次读取会报错:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#关闭文件
f.close()
#再次读取
f.read()
#python3 test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
f.read()
ValueError: I/O operation on closed file
file.fileno()
含义:fileno()方法返回一个整形的文件描述符,可用于底层操作系统的 I/O 操作。
语法:file.fileno()
返回值:无
以下实例演示了fileno()用法:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#输出文件描述符
fd = f.fileno()
print ("The FD is:",fd)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
The FD is: 3
file.istty()
含义:istty()方法检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False。
语法:file.istty():
返回值:连接到一个终端设备返回 True,否则返回 False。
以下实例演示了isatty()用法:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#输出文件描述符
ret = f.isatty()
print ("Return value:",ret)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Return value: False
file.next()
含义:next()该方法返回文件的下一行。
语法:next(file):
返回值:返回文件下一行。
以下实例演示了next()用法:
文件dpc内容如下:
我在一楼,我叫张三
我在二楼,我叫李四
我在三楼,我叫王五
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#循环读取文件
for i in range(3):
print ("number %s:" % (i),next(f))
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
number 0: 我在一楼,我叫张三
number 1: 我在二楼,我叫李四
number 2: 我在三楼,我叫王五
file.read()
含义:read()方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。
语法:file.read()
返回值:返回读取的字符串
参数:size #从文件中读取的字节数。
以下实例演示了 read() 方法的使用:
文件dpc内容如下:
Python
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#读取指定字节数
r = f.read(2)
print ("Returns a string:",r)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Returns a string: Py
file.readline()
含义:方法用于从文件读取整行,包括 "\n" 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。
语法:file.readline()
参数:size #从文件中读取的字节数。
以下实例演示了 readline() 方法的使用:
文件 runoob.txt 的内容如下:
www.baidu.com
www.google.com
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#读取整行
r = f.readline()
print ("Read the entire row:",r)
#读取指定字节数
r = f.readline(10)
print ("Number of bytes read:",r)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Read the entire row: www.baidu.comNumber of bytes read: www.google
file.readlines()
含义:方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结构进行处理。
语法:file.readlines()
返回值:返回列表,包含所有的行。
以下实例演示了 readline() 方法的使用:
文件 dpc 的内容如下:
www.baidu.com
www.google.com
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#返回列表值
r = f.readlines()
print ("Returns a list value:",r)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Returns a list value: ['www.baidu.com\n', 'www.google.com\n']
用for循环输出每行
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#for循环
for line in f.readlines():
line = line.strip()
print ("Read the entire row:",line)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Read the entire row: www.baidu.com
Read the entire row: www.google.com
file.seek()
含义:方法用于移动文件读取指针到指定位置。
语法:file.seek(offset,whence)
参数:offset #开始的偏移量,也就是代表需要移动偏移的字节数
whence: #可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前 位置开始算起,2代表从文件末尾算起。
以下实例演示了 readline() 方法的使用:
文件 dpc 的内容如下:
www.baidu.com
www.google.com
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#读取整行
r = f.readline()
print ("String:",r)
#设置文件读取指针
f.seek(14,0)
r = f.readline()
print ("String:",r)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
String: www.baidu.comString: www.google.com
file.tell()
含义:方法返回文件的当前位置,即文件指针当前位置。
语法:file.tell()
返回值:返回文件的当前位置。
以下实例演示了 tell() 方法的使用:
文件 dpc 的内容如下:
www.baidu.com
www.google.com
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#读取整行
r = f.readline()
print ("String:",r)
#当前位置
pos = f.tell()
print ("current location:",pos)
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
String: www.baidu.com
current location: 14
file.truncate()
含义:方法用于从文件的首行首字符开始截断,截断文件为 size 个字符,无 size 表示从当前位置截断;无 size 表示从当前位置截断;
语法:file.truncate(size)
参数:size #可选,如果存在则文件截断为 size 字节。
以下实例演示了 truncate() 方法的使用:
文件 runoob.txt 的内容如下:
Python
读取文件内容:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#截断字符串
f.truncate(2)
str = f.read()
print ("Read data: %s" % (str))
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpc
Read data: Py
file.write()
含义:方法用于向文件中写入指定字符串。
在文件关闭前或缓冲区刷新前,字符串内容存储在缓冲区中,这时你在文件中是看不到写入的内容的。如果文件打开模式带 b,那 写入文件内容时,str (参数)要用 encode 方法转为 bytes 形式,否则报错:TypeError: a bytes-like object is required, not 'str'。
语法:file.write(str)
参数: str #要写入文件的字符串
返回值:返回的是写入的字符长度。
以下实例演示了 tell() 方法的使用:
文件 dpc 的内容如下:
www.baidu.com
www.google.com#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#输出文件名
print ("The file name is:",f.name)
#在文件末尾写入一行
f.seek(0, 2)
line = f.write( str )
#读取文件所有内容
f.seek(0,0)
for index in range(3):
line = next(f)
print ("Number %d - %s" % (index, line))
#关闭文件
f.close()
# python3 test.py
The file name is: /root/dpcNumber 0 - www.baidu.com
Number 1 - www.google.com
Number 2 - www.runoob.com
file.writelines()
含义:方法用于向文件中写入一序列的字符串。这一序列字符串可以是由迭代对象产生的,如一个字符串列表。换行需要制定换行符 \n。
语法:file.wrttelines(str)
参数:str #要写入文件的字符串序列。
以下实例演示了 writelines() 方法的使用:
#!/usr/bin/python3
#打开文件
f = open("/root/dpc","r")
#写入数据
seq = ['www.baidu.com \n','www.google.com \n']
f.writelines(seq)
#关闭文件
f.close()
# python3 test.py
查看文件内容:
# cat /root/dpc
www.baidu.com
www.google.com