文件操作的步骤
打开文件
对文件进行操作
关闭文件
打开文件
open("filename" [, mode])
codecs.open("filename"[, mode])
需import codecs 模块 (处理编码转换),推荐使用
两者均返回一个 file 对象
打开方式 Mode(默认只读方式 rb)
r 读
w 写(覆盖)
b 二进制
a 追加
import codecs
file = open("1.txt")
print(type(file))
file1 = codecs.open('1.txt')
print(type(file1))
执行:
C:Python27python.exe D:/Python/Study/file-1.py
<type 'file'>
<type 'file'>
Process finished with exit code 0
关闭文件
close()
关闭打开的文件
closed()
检测文件是否关闭 返回布尔值
import codecs
file = codecs.open('1.txt')
print(type(file))
print(file.closed)
file.close()
print(file.closed)
执行:
C:Python27python.exe D:/Python/Study/file-1.py
<type 'file'>
False
True
Process finished with exit code 0
with
import codecs
with codecs.open('1.txt') as file:
print(file.closed)
print(file.closed)
等价于
import codecs
file = codecs.open('1.txt')
print(file.closed)
file.close()
print(file.closed)
操作
test.txt 内容
aaaa
#################
bbbb
@@@@@@@@@@@@@@@@@
cccc
$$$$$$$$$$$$$$$$$
dddd
读文件
file.read()
读取整个文件,返回一个字符串类型(可对其使用字符串的所有方法)
iimport codecs
file = codecs.open("test.txt")
text = file.read()
print(type(text))
print(text)
result = text.replace("a", "1")
print(result)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
<type 'str'>
aaaa
#################
bbbb
@@@@@@@@@@@@@@@@@
cccc
$$$$$$$$$$$$$$$$$
dddd
1111
#################
bbbb
@@@@@@@@@@@@@@@@@
cccc
$$$$$$$$$$$$$$$$$
dddd
Process finished with exit code 0
file.readlines(self,size=None)
读取所有行,返回一个列表,每一行为列表里的一个元素
readlines([size]) -> list of strings, each a line from the file.
重复调用readline(),并返回所读行的列表。可选的size参数(如果给定)是返回行中总字节数的近似边界。
import codecs
file = codecs.open('test.txt')
text = file.readlines()
print(type(text))
print(text)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
<type 'list'>
['aaaa
', '#################
', 'bbbb
', '@@@@@@@@@@@@@@@@@
', 'cccc
', '$$$$$$$$$$$$$$$$$
', 'dddd']
Process finished with exit code 0
file.readline(self,size=None) 和 file.next()
file.readline() 逐行读取文件,返回一个字符串
readline([size]) -> next line from the file, as a string.
保留换行符非负大小参数限制返回的最大字节数(然后可能会返回不完整的行)。在EOF返回一个空字符串
import codecs
file = codecs.open('test.txt')
text = file.readline()
print(type(text))
print(file.readline())
print(file.readline())
print(file.readline())
print(text)
# file.next()
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
<type 'str'>
#################
bbbb
@@@@@@@@@@@@@@@@@
aaaa
Process finished with exit code 0
file.next() 读取下行
import codecs
file = codecs.open('test.txt')
print(file.readline())
print(file.next())
file.close()
执行;
C:Python27python.exe D:/Python/file/file.py
aaaa
#################
Process finished with exit code 0
写文件
file.write(filename,[wb/ab])
需要传字符串类型的数据
#写入一个文件
import codecs
file = codecs.open('1.txt', 'wb')
# text = file.read()
# print(text)
# print('#' *30)
file.write('this is a test file for python write!!!')
print('#' *30)
file.close()
file = codecs.open('1.txt')
text = file.read()
print(text)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
##############################
this is a test file for python write!!!
Process finished with exit code 0
#覆盖内容
import codecs
file = codecs.open('1.txt','rb')
text = file.read()
print(text)
file.close()
file = codecs.open('1.txt','wb')
file.write('1.txt已存在,wb模式将覆盖其原有内容')
file.close()
file = codecs.open('1.txt','rb')
text = file.read()
print(text)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
this is a test file for python write!!!
1.txt已存在,wb模式将覆盖其原有内容
Process finished with exit code 0
# 追究加
import codecs
file = codecs.open('1.txt','rb')
text = file.read()
print(text)
file.close()
file = codecs.open('1.txt','ab')
file.write('这些内容将追加到1.txt中')
file.close()
file = codecs.open('1.txt','rb')
text = file.read()
print(text)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
1.txt已存在,wb模式将覆盖其原有内容
1.txt已存在,wb模式将覆盖其原有内容这些内容将追加到1.txt中
Process finished with exit code 0
file.writelines()
必须传入一个列表
import codecs
file = codecs.open('2.txt', 'ab')
file.writelines(['123121
', 'AFSDFA
', '3424wewersf
'])
file.close()
file = codecs.open('2.txt', 'rb')
print(file.read())
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
123121
AFSDFA
3424wewersf
Process finished with exit code 0
其它方法
file.tell()
返回文件的当前位置,即文件指针当前位置
import codecs
file = codecs.open('test.txt')
text = file.read()
print(text)
print(file.tell())
file.close()
print('#' * 30)
file = codecs.open('test.txt')
line = file.readline()
print(line)
print(file.tell())
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
aaaa
#################
bbbb
@@@@@@@@@@@@@@@@@
cccc
$$$$$$$$$$$$$$$$$
dddd
79
##############################
aaaa
6
Process finished with exit code 0
file.seek() fileObject.seek(offset[, whence])
用于移动文件读取指针到指定位置 没有返回值。
offset -- 开始的偏移量,也就是代表需要移动偏移的字节数
whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
import codecs
file = codecs.open('test.txt')
print(file.readline())
file.seek(0, 0)
print(file.readline())
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
aaaa
aaaa
Process finished with exit code 0
file.name()
import codecs
file = codecs.open('test.txt')
print(file.name)
file.close()
执行:
C:Python27python.exe D:/Python/file/file.py
test.txt
Process finished with exit code 0