一、文件
f = open('E:\\Record.txt') print(f.read()) print(f.read()) # 已经读取过了,再读取一遍是无法读取出来的。 f.close() f = open('E:\\Record.txt') # 关闭重新打开 print(f.read(48)) print(f.tell()) # 当前文件指针的位置。 """ 读取前面48个字符,我凑齐一句话写的48。 但是很明显的问题就是指针的位置匹配不上, 如果我这里写的读取范围是47,49等的话,那么就是正常的。 反正这里的48不知道是不是因为”.“后面的位置不确定,所以这里就按照最大值来返回指针位置了。 换行符的位置倒是不会遇到这种情况。 另外还有一点就是,如果读去了一段乱码的中文近来,那么显示指针位置的时候,一个中文字符是要占据两个字节长度的。""" print(f.seek(50, 0)) # 从开始找到第50个字节的位置。 print(f.readline()) # 从第50个字节的位置打印出一行。 print(list(f)) # 将文本转化为列表。 f.seek(0, 0) # 指针归0 lines = list(f) for each_line in lines: print(each_line) # 把文档按行打印出来,但是数据庞大的时候效率不高 f.seek(0, 0) for each_line in f: print(each_line) # 可以直接用f迭代出来原文件,不用写成列表。 f = open('E:/Text', 'w') f.write('This is a test!') f.close()
运行结果:
1,This is a text to pratice the openning a file.
2,Think about how to spend my National’s Day.
3,I know that I can only stay in school.::>_<::
4,But it’s not so bad.
5,I can go to the liboratory and watch the experiments.
6,I believe I can learn something new.
7,I can also come to the liborary everyday.
8,So that’s probably my National’s Day’s schedule
9,A little bit boring,but I’m not wasting my time.^_^
Ok,the testing result is that Python cannot read Chinese words and some complicated emotion.
1,This is a text to pratice the openning a file.
18446744073709551665
50
2,Think about how to spend my National’s Day.
[‘3,I know that I can only stay in school.::>_<::\n’, “4,But it’s not so bad.\n”, ‘5,I can go to the liboratory and watch the experiments.\n’, ‘6,I believe I can learn something new.\n’, ‘7,I can also come to the liborary everyday.\n’, “8,So that’s probably my National’s Day’s schedule\n”, “9,A little bit boring,but I’m not wasting my time.^_^\n”, ‘\n’, ‘\n’, ‘Ok,the testing result is that Python cannot read Chinese words and some complicated emotion.’]
1,This is a text to pratice the openning a file.
2,Think about how to spend my National’s Day.
3,I know that I can only stay in school.::>_<::
4,But it’s not so bad.
5,I can go to the liboratory and watch the experiments.
6,I believe I can learn something new.
7,I can also come to the liborary everyday.
8,So that’s probably my National’s Day’s schedule
9,A little bit boring,but I’m not wasting my time.^_^
Ok,the testing result is that Python cannot read Chinese words and some complicated emotion.
1,This is a text to pratice the openning a file.
2,Think about how to spend my National’s Day.
3,I know that I can only stay in school.::>_<::
4,But it’s not so bad.
5,I can go to the liboratory and watch the experiments.
6,I believe I can learn something new.
7,I can also come to the liborary everyday.
8,So that’s probably my National’s Day’s schedule
9,A little bit boring,but I’m not wasting my time.^_^
Ok,the testing result is that Python cannot read Chinese words and some complicated emotion.
Process finished with exit code 0