Read the contents of the file --open function

Reference: https://docs.python.org/3/library/functions.html?highlight=open#open

Python to read and write files in two ways: open function and file type, in which the class file open function itself is called , For normal operation, the official recommended to use open function instead of the file class. #------------------------------------------------------------------------------- The open function supports read and write file operations, using a different, indicates a read and write different modes: x write mode, create a new file, if the file already exists it will error. r to open the file in read-only mode. Pointer file will be placed at the beginning of the file. This is the default mode. w opens a file for writing only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing. b binary mode. + Open a file is updated (read and write). It supports both read and write mode.

 

---------------------------------------------------------------------------------------------------------------------------------------------------

open(filemode='r'buffering=-1encoding=Noneerrors=Nonenewline=Noneclosefd=Trueopener=None)

 

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



 

 




A, read ([size]) Method Read ([size]) method reads the current position from the file size bytes, if no size parameter, until the end of the file to read, which indicates the range of a String object; read () read directly into a string of bytes, including the line break;
Open = F ( ' C: \\ Desktop the Users \\ \\ \\ del new text document 5.txt ' )

s = f.read()
f.close()


print(type(s))
print(s)

Results of the:

<class 'str'>
111111111
中国
22222
美国
33333

55555555
俄罗斯
6666666

 

====================================================================================================================

 

二、readline()方法

从字面意思可以看出,该方法每次读出一行内容,所以,读取时占用内存小,比较适合大文件,该方法返回一个字符串对象。


readline()  读取整行,包括行结束符,并作为字符串返回
f = open('C:\\Users\\del\\Desktop\\新建文本文档5.txt')

s = f.readline()
f.close()


print(type(s))
print(s)

执行结果:

<class 'str'>
111111111

 

===============================================================================================

 

三、readlines()方法

读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素,但读取大文件会比较占内存


特点:一次性读取整个文件;自动将文件内容分析成一个行的列表。


readlines()读取所有行然后把它们作为一个字符串列表返回。
f = open('C:\\Users\\del\\Desktop\\新建文本文档5.txt')

s = f.readlines()
f.close()


print(type(s))
print(s)

执行结果:

<class 'list'>
['111111111\n', '中国\n', '22222\n', '美国\n', '33333\n', '\n', '55555555\n', '俄罗斯\n', '6666666\n']

 

===========================================================================================================

 

切片操作:

f = open('C:\\Users\\del\\Desktop\\新建文本文档5.txt')

s = f.readlines()
f.close()


print(type(s))
print(s)

for line in s:
    print(line[:-1])                        #<class 'str'>

 

执行结果:

 

<class 'list'>
['111111111\n', '中国\n', '22222\n', '美国\n', '33333\n', '\n', '55555555\n', '俄罗斯\n', '6666666\n']
111111111
中国
22222
美国
33333

55555555
俄罗斯
6666666

 

=================================================================================

切片,并存入列表中

f = open('C:\\Users\\del\\Desktop\\新建文本文档5.txt')

s = f.readlines()
f.close()


print(type(s))
print(s)

users = []

for line in s:
    print(line[:-1])  #<class 'str'>
    users.append(line[:-1])

print(users)

print(users[0])
print(users[1])
print(users[2])
print(users[5])
print(type(users[5]))
print(len(users[5]))
if users[5] == '':
    print('')

 

 

执行结果:

 

<class 'list'>
['111111111\n', '中国\n', '22222\n', '美国\n', '33333\n', '\n', '55555555\n', '俄罗斯\n', '6666666\n']
111111111
中国
22222
美国
33333

55555555
俄罗斯
6666666
['111111111', '中国', '22222', '美国', '33333', '', '55555555', '俄罗斯', '6666666']
111111111
中国
22222

<class 'str'>
0

 

Guess you like

Origin www.cnblogs.com/xiaobaibailongma/p/12375515.html