Python 15 study notes text - reading and writing

In our programming, we often need to file read and write operations.

In Python, read and write the text is very convenient, just a few simple lines of code can be achieved.

We first create a text file "Text.txt", there are a lot of content, we will try to read it all out

with open('Text.txt') as file_object:
    contents = file_object.read()
    print(contents)
    
    
    
'''
输出:
查询出系统中最慢的SQL语句
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 20
CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2))
AS [Total Elapsed Duration(s)]
, qs.execution_count
, SUBSTRING (qt.text, (qs.statement_start_offset/2) + 1,
.....
.....

'''

 

We see the use of open way, we can open a text object, and through the read function, the contents of the text read out all the. And other languages ​​different is that we do not use the code text will close off.

Python will automatically manage text when no longer in use or the right time to turn it off, of course, you can manually turn it off to conserve resources.

with open('Text.txt') as file_object:
    contents = file_object.read()
    print(contents)
    file_object.close()

 

Reads the text line by line:

with open('Text.txt') as file_object:
    for line in file_object:
        print(line)
    file_object.close()
'''
输出:
查询出系统中最慢的SQL语句
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 20
CAST(qs.total_elapsed_time / 1000000.0 AS DECIMAL(28, 2))
AS [Total Elapsed Duration(s)]
, qs.execution_count
, SUBSTRING (qt.text, (qs.statement_start_offset/2) + 1,
.....
.....

'''

 

Use the table of contents stored in the text, we can use readlines read out the text line by line, and assigned to a list, use other code is:

with open('Text.txt') as file_object:
    lineslist = file_object.readlines()
    file_object.close()

print(len(lineslist))
print(lineslist[1])
'''
输出:
187
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

'''

 

We can simply read the file by the above method.

Then we can try to write a text: 

with open('write.txt', 'w') as file_object:
    file_object.write('hello world!')
    file_object.close()


'''
输出:
hello world!

'''

 

The above code, we try to write a text called 'write.txt' text in the same directory python in a 'hello world!'. If the file does not exist, python will create a new default.

We can also write to the file multiple lines to the file, note the use of line breaks:

with open('write.txt', 'w') as file_object:
    file_object.write('hello world!\n')
    file_object.write('hello ralf!\n')
    file_object.write('hello rachel!\n')
    file_object.write('hello wowo!\n')
    file_object.close()


'''
输出:
hello world!
hello ralf!
hello rachel!
hello wowo!

'''

 

Note that using the 'w' mode, the text will be overwritten, so we can use 'a' append mode, the contents of the file additional:

with open('write.txt', 'w') as file_object:
    file_object.write('hello world!\n')
    file_object.write('hello ralf!\n')
    file_object.write('hello rachel!\n')
    file_object.write('hello wowo!\n')
    file_object.close()

with open('write.txt', 'a') as file_object:
    file_object.write('hello Python!\n')
    file_object.close()
'''
输出:
hello world!
hello ralf!
hello rachel!
hello wowo!
hello Python!
'''

 

Through the above code, we can see that, in fact, we operate on the text is to create a text object by open, choose to read, write, append and other related operations according to different modes:

file object = open(file_name [, access_mode][, buffering])

file_name: file_name variable is a string value containing the name of the file you want to access.
access_mode: access_mode decided to open the file mode: Read, Write, and additions. See a complete list of all possible values as follows. This parameter is not mandatory, the default file is read-only access mode (r).
buffering: If the value of buffering is set to 0, there will be storage. If the value of buffering take 1, the line will register to access the file. If the value is an integer greater than 1 of buffering, indicating that this is the buffer size of the storage area. If negative, the buffer size for the system default parking zone.

Other relevant parameters and the process is as follows:

The following comes from:
Author: cat blankly
Source: CSDN
Original: https: //blog.csdn.net/jiaoyangwm/article/details/79635271
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

  • Mode Description
  • 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.
  • rb Open a binary file for read-only format. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as pictures and so on.
  • r + open a file for reading and writing. The file pointer will be placed at the beginning of the file.
  • rb + open a file for reading and writing binary format. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures and so on.
  • w opens a file for writing only. If the file already exists it will be overwritten. If the file does not exist, create a new file.
  • wb open a file for writing in binary format only. If the file already exists it will be overwritten. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
  • w + open a file for reading and writing. If the file already exists it will be overwritten. If the file does not exist, create a new file.
  • wb + opens a file for reading and writing binary format. If the file already exists it will be overwritten. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
  • 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.
  • ab open a file in binary format for additional. 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.
  • a + open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. It would append mode when the file is opened. If the file does not exist, create a new file for reading and writing.
  • ab + opens a file for additional binary format. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

 

 

 

Guess you like

Origin www.cnblogs.com/wanghao4023030/p/11221385.html