python3 file stream

File stream

# Python inside to open a file with open (), if not open, will throw an exception: OSError

The basic parameters of the file stream #
# 1 . File: Open a File
# 2 the MODE:. Open mode, the default mode is the mode txt
# 3 . Buffering: Buffer Set
# . 4 encoding:. Character encoding, usually with UTF- . 8 
# . 5 errors:. Given level
# 6 . NEWLINE: distinguish line break
# 7 closefd:. Incoming file parameter types
# 8. opener:


# Mode details
# R read-only
R & lt # +     to open a file for reading and writing. The file pointer will be placed at the beginning of the file.

# W write only
W # +     to open a file for reading and writing. 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.

# A Add append
A # +     read, 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.



f = open('test.txt',encoding='utf-8')
count = 0
for i in f:
    # Modify the contents of line 10 of the judgment. This judgment can not be directly, so we need to add a counter
    count += 1
    if count == 9:
        Print ( ' dividing lines ' .center ( 50 , ' - ' ))
         Continue
    print(i.strip())
f.close()

 

Guess you like

Origin www.cnblogs.com/yeyu1314/p/11990753.html