一、打开文件
file(name[, mode[, buffering]]) -> file object 或
open(name[, mode[, buffering]]) -> file object
1、'r'读方式。
2、'w'写方式。打开文件时,就把文件给清空了。从第一行开始写,如果原文件有内容,会被覆盖。
3、'w+'可读可写,如果没有文件则创建文件。打开文件时,就把文件给清空了。从第一行开始写,如果原文件有内容,会被覆盖。
4、'a'追加方式去可读可写,如果没有文件则创建文件。文件打开时不清空原文件内容,在文件末尾写入文件。
5、'b'二进制方式。
>>> f1 = file('F:\\PYTHON\\Python Developing\\file1.txt','a')
>>> f1.write('''\
hello world
python
''')
>>> f1 = open('F:\\PYTHON\\Python Developing\\file1.txt','w')
>>> f1.write('''\
hello world
python
''')
>>> f1.close()
>>>
二、读写文件
1、read全部读回来,返回字符串。
read(5),只读5个字符的内容。
2、readline只对一行,返回字符串。在读回来的末尾自动加了\n。
去除\n:
>>> f1.close()
>>> f1 = file('F:\\PYTHON\\Python Developing\\file1.txt','r')
>>> line = f1.readline()
>>> f1.close()
>>> print line
hello world
>>> f1 = file('F:\\PYTHON\\Python Developing\\file1.txt','r')
>>> line = f1.readline()
>>> line = line.rstrip('\n')
>>> f1.close()
>>> print line
hello world
>>>
3、readlines全部读回来,返回一个字符串列表,每行是这个列表的元素。
>>> f1 = file('F:\\PYTHON\\Python Developing\\file1.txt','r')
>>> lines = f1.readlines()
>>> print lines
['hello world\n', 'python\n']
>>> f1.close()
>>>
4、write
>>> s1 = '22345678'
>>> s2 = '22345678'
>>> s3 = '32345678'
>>> f = file(r'F:\PYTHON\Python Developing\file1.txt','w+')
>>> f.write(s1 + '\n')
>>> f.write(s2 + '\n')
>>> f.write(s3 + '\n')
>>> s4 = '42345678\n'
>>> f.write(s4)
>>> f.close()
>>> f = file(r'F:\PYTHON\Python Developing\file1.txt','r')
>>> s = f.read()
>>> print s
22345678
22345678
32345678
42345678
5、writelines
>>> help(file.writelines)
Help on method_descriptor:
writelines(...)
writelines(sequence_of_strings) -> None. Write the strings to the file.
Note that newlines are not added. The sequence can be any iterable object
producing strings. This is equivalent to calling write() for each string.
>>> s1 = '12345678\n'
>>> s2 = '22345678\n'
>>> s3 = '32345678\n'
>>> s4 = '42345678\n'
>>> f = file(r'F:\PYTHON\Python Developing\file1.txt','w')
>>> f.writelines([s1,s2,s3,s4])
>>> f.close()
>>> f = file(r'F:\PYTHON\Python Developing\file1.txt','r')
>>> s = f.read()
>>> f.close()
>>> print s
12345678
22345678
32345678
42345678
>>>
三、关闭文件
f.close(),只有关闭之后,才会真正写到文件里面去。
四、格式化写入文件。
>>> f = open(r'F:\PYTHON\Python Developing\file2.txt','w+')
>>> head = '%10s%10s%10s\n'%('NAME','N.O','RECORD')
>>> line1 = '%10s%10d%10.2f\n'%('xiaoming',000001,98.92)
>>> line2 = '%10s%10d%10.2f\n'%('xiaohong',000002,94)
>>> line3 = '%10s%10d%10.2f\n'%('xiaoli',000003,85.9)
>>> line4 = '%10s%10d%10.2f\n'%('xiaoqin',000004,99.96)
>>> f.write(head)
>>> f.write(line1)
>>> f.write(line2)
>>> f.write(line3)
>>> f.write(line4)
>>> f.close()
>>> f = open(r'F:\PYTHON\Python Developing\file2.txt','r')
>>> s = f.read()
>>> f.close()
>>> print s
NAME N.O RECORD
xiaoming 1 98.92
xiaohong 2 94.00
xiaoli 3 85.90
xiaoqin 4 99.96
>>>
五、while循环读含空行的文件。
空行是只有空白字符的行,或者是空字符串的行,即仅包含空格、\t、\n。判断一行是否为空行方法:len(line.strip()) == 0。
最后一行是不含\n的行,因为只要读到文本最后有'\n'的时候,都会另起一行。
xiaohong后面增加一个空行,s读到\n,非空,继续while。
>>> f = open(r'F:\PYTHON\Python Developing\file2.txt','r')
>>> s = f.readline()
>>> while s != '':
s = s.rstrip('\n')
print s
s = f.readline()
NAME N.O RECORD
xiaoming 1 98.92
xiaohong 2 94.00
xiaoli 3 85.90
xiaoqin 4 99.96
>>> f.close()
>>>
六、for循环可以遍历迭代器对象有String、List、Tuple、Dict、Set、File。
>>> f = open(r'F:\PYTHON\Python Developing\file2.txt','r')
>>> for s in f:
s = s.rstrip('\n')
print s
NAME N.O RECORD
xiaoming 1 98.92
xiaohong 2 94.00
xiaoli 3 85.90
xiaoqin 4 99.96
>>> f.close()
>>>
七、pythonic编程用with open。
>>> #coding:gbk
>>>
>>> file_name = r'F:\PYTHON\Python Developing\Bigfile.txt'
>>> i = 0
>>> with open(file_name,'a') as f:
while i < 200000:
s = '%60s%10d\n'%('Python [2018-01-03 20:30:40] 是门非常容易学习的语言。' ,i)
i = i + 1
f.write('\r\n')
f.write(s)
>>>