python 8

1. Initial knowledge of file operation

1.

path file path F:\file.txt

encoding encoding utf-8, gbk ...

mode Operation mode read-only, write-only, read-write, write-read, append...

f1 = open(r'F:\文件.txt', encoding='utf-8', mode='r')
print(f1.read())
f1.close()

Absolute path: Find files all the way from the root directory.

Relative paths: files found starting from the current directory.

2. r w a

Read mode:

r read only

rb directly reads the bytes type

f1 = open( ' World Peace.txt ' , mode= ' rb ' )
 print (f1.read())
f1.close()

 

r+ read and write

r+b directly reads and writes the bytes type

 

Write mode:

w Write only If the file does not exist, create the file. If the file exists, delete the file first, and then create the file.

f1 = open(r ' World Peace.txt ' , encoding= ' utf-8 ' , mode= ' w ' )
f1.write( ' Xue Zhiqian's son ' )
f1.close()

 

wb directly writes the bytes type

f1 = open(r ' World Peace.txt ' , mode= ' wb ' )
f1.write( ' Xue Zhiqian's son'.encode( ' utf-8 ' ) )
f1.close()

 

w+ write read 

w+b bytes type write and read

 

Append mode:

a Append If there is no file, create a file to write the content, if there is a file, append the content directly after the file.

ab a+ a+b is the same. .

 

For letters, numbers, and some common special characters, gbk and utf-8 both extend the writing method of ASCII code, so they can be used in common with each other.

Second, the common operation of the file.

1..read()    .close()

Common file handles: f1_obj, f1_file, file, file_handle...

The default encoding method of windows is (exclusive encoding of the current country, gbk in China)

The default encoding for Linux and macOS is: utf-8

2. read(n) How many xx are read. In r mode, it is read according to characters, and in rb mode, it is read according to bytes.

3.readline() Read line by line and move the cursor to the beginning of the line

4.readlines() returns a list, the elements in it are each line

5. The for loop reads, one line at a time

6.seek() adjusts the cursor according to the byte. When the file is written, the cursor will move to the end, and the cursor needs to be adjusted to continue reading.

The reading and writing of non-text data can only be performed with related operations of the bytes type.

7.readable() writeable() to determine whether it is readable and writable

8.tell() tells the position of the cursor

9. truncate() truncates the original file and can only be used in a mode. in bytes.

Three, another method of operation of the file

 

with open(r'F:\世界和平.txt',encoding='utf-8', mode='r')as f1:
    print(f1.read())

This command will automatically close the file in a short time, but if you use with open() as to operate the same file under with op() as, it is best to close it manually before, because the same file cannot be defined as different handles.

'''
1. Open the original file old_file and read the original content into memory.
2. Create a new file new_file.
3. Rewrite the original content to form new content and write it to a new file.
4. Delete the original file.
5. Rename the new file to the original file.
'''
#方法一,原文件内容不打,可以用此方法,但是此方法还是很low。
# import os
# with open('change', encoding='utf-8') as f1,\
#     open('change.bak', encoding='utf-8', mode='w') as f2:
#     old_content = f1.read()
#     new_content = old_content.replace('alex', 'SB')
#     f2.write(new_content)
# os.remove('change')
# os.rename('change.bak', 'change')
# 方法2
import os
with open('change', encoding='utf-8') as f1,\
    open('change.bak', encoding='utf-8', mode='w') as f2:
    for line in f1:
        new_line = line.replace('SB', 'alex')
        f2.write(new_line)
os.remove('change')
os.rename('change.bak', 'change')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325887654&siteId=291194637