python中的文件内容操作
一、读文件内容
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
注:不能把open语句放在try块里,因为当打开文件出现异常时,文件对象file_object无法执行close()方法。
list_of_all_the_lines = file_object.readlines( )
file = open('test.log', 'r')
sizehint = 2097152
position = 0
lines = file.readlines(sizehint)
while not file.tell() - position < 0:
position = file.tell()
lines = file.readlines(sizehint)
如果文件是文本文件,还可以直接遍历文件对象获取每行:
for line in file_object:
process line
input = open('data', 'rb')
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
二、写文件
写文本文件
output = open('data', 'w')
写二进制文件
output = open('data', 'wb')
追加写文件
output = open('data', 'w+')
写数据
file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )
写入多行
file_object.writelines(list_of_text_strings)
注意,调用writelines写入多行在性能上会比使用write一次性写入要高。
fp = file('data.txt')
lines = []
for line in fp:
lines.append(line)
fp.close()
lines.insert(1, 'a new line')
s = '\n'.join(lines)
fp = file('data.txt', 'w')
fp.write(s)
fp.close()
四、file对象的属性和方法
1.属性:
closed
encoding
mode
name
newlines
softspace
2.file的读写方法:
F.read([size])
F.readline([size])
F.readlines([size])
F.write(str)
F.writelines(seq)
3.file的其他方法:
F.close()
F.flush()
F.fileno()
F.isatty()
F.tell()
F.next()
F.seek(offset[,whence])
whence为0时表示从文件头部开始向后移动“偏移量”字节。
whence为1时表示以当前位置为原点向后移动“偏移量”字节。
whence为2时表示以文件末尾为原点向前移动“偏移量”字节。
需要注意,如果文件以a或a+的模式打开,每次进行写操作时,文件操作标记会自动返回到文件末尾。
F.truncate([size])
五、读写文件的基本Demo
import os,sys
try:
fsock = open("D:/SVNtest/test.py", "r")
except IOError:
print "The file don't exist, Please double check!"
exit()
print 'The file mode is ',fsock.mode
print 'The file name is ',fsock.name
P = fsock.tell()
print 'the postion is %d' %(P)
fsock.close()
fsock = open("D:/SVNtest/test.py", "r")
AllLines = fsock.readlines()
for EachLine in fsock:
print EachLine
print 'Star'+'='*30
for EachLine in AllLines:
print EachLine
print 'End'+'='*30
fsock.close()
fsock = open("D:/SVNtest/test.py", "a")
fsock.write("""
#Line 1 Just for test purpose
#Line 2 Just for test purpose
#Line 3 Just for test purpose""")
fsock.close()
S1 = fsock.closed
if True == S1:
print 'the file is closed'
else:
print 'The file donot close'
六、文件分割(按大小)
import sys,os
kilobytes = 1024
megabytes = kilobytes*1024
chunksize = int(200*megabytes)
def split(fromfile,todir,chunksize=chunksize):
if not os.path.exists(todir):
os.mkdir(todir)
else:
for fname in os.listdir(todir):
os.remove(os.path.join(todir,fname))
partnum = 0
inputfile = open(fromfile,'rb')
while True:
chunk = inputfile.read(chunksize)
if not chunk:
break
partnum += 1
filename = os.path.join(todir,('part%04d'%partnum))
fileobj = open(filename,'wb')
fileobj.write(chunk)
fileobj.close()
return partnum
if __name__=='__main__':
fromfile = input('File to be split?')
todir = input('Directory to store part files?')
chunksize = int(input('Chunksize to be split?'))
absfrom,absto = map(os.path.abspath,[fromfile,todir])
print('Splitting',absfrom,'to',absto,'by',chunksize)
try:
parts = split(fromfile,todir,chunksize)
except:
print('Error during split:')
print(sys.exc_info()[0],sys.exc_info()[1])
else:
print('分割完成:',parts,'parts are in',absto)
七、合并文件
import sys,os
def joinfile(fromdir,filename,todir):
if not os.path.exists(todir):
os.mkdir(todir)
if not os.path.exists(fromdir):
print('Wrong directory')
outfile = open(os.path.join(todir,filename),'wb')
files = os.listdir(fromdir)
files.sort()
for file in files:
filepath = os.path.join(fromdir,file)
infile = open(filepath,'rb')
data = infile.read()
outfile.write(data)
infile.close()
outfile.close()
if __name__=='__main__':
fromdir = input('Directory containing part files?')
filename = input('Name of file to be recreated?')
todir = input('Directory to store recreated file?')
try:
joinfile(fromdir,filename,todir)
except:
print('Error joining files:')
print(sys.exc_info()[0],sys.exc_info()[1])
问题1:你知道怎么快速的在文本文件中间指定行插入一行吗?
这个问题在后面详细讨论。
问题2:文件的删除、复制、剪切、遍历该怎么操作?
这需要用到os模块,详细的在另外的文章中总结。
import os
os.remove(file)