文件内建方法
open()成功执行并返回一个文件对象之后,所有对该文件的后续操作都将通过这个“句柄”进行。
文件方法可以分为4类:输入、输出、文件内移、杂项操作
输入:
1.read()方法用来直接读取字节到字符串中,最多读取给定数目个字节。如果没有给定size 参数(默认为-1)或者size值为负数,文件将被读取知道文件末尾
believe in yourself
you can learn python
file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.read() print(all_the_text )
C:\Users\minkl\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/minkl/PycharmProjects/untitled/Open.py
believe in yourself
you can learn python
Process finished with exit code 0
2.readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节)。然后整行,包括行结束符,作为字符串返回。
和read()相同,它也有一个可选的size参数,默认为-1,代表读至行结束符。
如果提供了该参数,那么在超过size个字节之后会返回不完整的行。
file_object = open(r"d:\78260\test\test.txt") ##如果没有给定size 参数(默认为-1)或者size值为负数,文件将被读取直到文件末尾all_the_text = file_object.readline()print(all_the_text )
believe in yourselffile_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readline(-2) print(all_the_text )believe in yourself
file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readline(-8) print(all_the_text ) believe in yourselffile_object = open(r"d:\78260\test\test.txt") ##如果提供了该参数,那么在超过size个字节之后会返回不完整的行。 all_the_text = file_object.readline(12) print(all_the_text ) believe in y 3.readlines()方法并不像其它两个输入方法一样返回一个字符串。 他会读取所有(剩余的)行然后把他们作为一个字符串列表返回。file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readlines(1) print(all_the_text ) ['believe in yourself \n'] ##读取除了参数之外剩余的行,并作为列表返回。file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readlines() print(all_the_text )
['believe in yourself \n', 'you can learn python ']file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readlines(20) print(all_the_text ) ['believe in yourself \n']file_object = open(r"d:\78260\test\test.txt") all_the_text = file_object.readlines(21) print(all_the_text )['believe in yourself \n', 'you can learn python '] 输出: 1.write()内建方法的功能与read()和readline()相反。他把含有文本数据或二进制数据块的字符串写到文件中去。 把str写到文件中,默认是不加换行符的,所以如果想换行的话,得手动加入换行符'\n'。file_object = open("d:/78260/test/test.txt","w+") #all_the_text = file_object.read() write_the_text = file_object.write('believe in yourself \n you can study python well') #print(all_the_text ) print(write_the_text ) believe in yourself you can study python wellfile_object = open("d:/78260/test/test.txt","w+") #all_the_text = file_object.read() write_the_text = file_object.write('believe in yourself you can study python well') #print(all_the_text ) print(write_the_text )believe in yourself you can study python well
###你可以反复调用write()
来写入文件,但是务必要调用f.close()
来关闭文件。
当我们写文件时,操作系统往往不会立刻把数据写入磁盘,而是放到内存缓存起来,空闲的时候再慢慢写入。
只有调用close()
方法时,操作系统才保证把没有写入的数据全部写入磁盘。
忘记调用close()
的后果是数据可能只写了一部分到磁盘,剩下的丢失了。
2.、writelines(seq)seq:把seq(序列)的内容全部写到文件中(多行一次性写入)。也不会自动加入换行符。注意:序列中的内容也必须是字符串类型的数据,才能成功写入文件。file_object = open("d:/78260/test/test.txt","w+") #all_the_text = file_object.read() write_the_text = file_object.writelines('believe in yourself \n you can study python well') #print(all_the_text ) print(write_the_text ) believe in yourself you can study python well