Follow + star, learn new Python skills every day

From the Internet, intrusion and deletion

File is an abstract concept of data storage, which is usually used for long-term storage of data. It can be of various types, suffixes, and encoding methods. Reading and writing files is one of the most commonly used operations. Python has built-in open()methods for reading and writing files. The basic syntax format is:

f = open(filename, mode)

open()The method returns a file object, which can be assigned to a variable. read()All objects with and methods in Python write()can be considered as an object of type file. Open the file and perform the corresponding operation. When the operation is complete, you need to use the close()method to close the file.

In Python's file reading and writing, there are the following considerations:

1. If the file is open()opened with a method, it must be close()closed with a method, otherwise the last written content may not be saved.

fw = open('测试文件.txt', 'wt')
for i in range(10000):
fw.write('{} '.format(i))

The above program writes the numbers to the file from 0 to 9999, and separates each number with a space, but does not close()close the fw file object with a method. The result is as shown in the figure below, only 8416 is written, The latter is lost. The system does cache processing when the file is read and written. The write is not written to the hard disk immediately, but is first written to the cache in the memory. When the cache area is full, it will be officially written to the hard disk, and the last content will be cached. It will close()be written to the hard disk at the same time, and close()the method is called in the flush()method. If it is not called close(), this part of the data will be lost.

picture

2. There are many ways to open files. The default is read-only mode. The following table lists other common read-write modes.

open mode

describe

r

Read-only mode, the default value, if the file does not exist, return the exception FileNotFoundError

w

Overwrite mode, if the file does not exist, it will be created, and if it exists, the source file will be completely overwritten

a

Append write mode, if the file does not exist, it will be created, and if it exists, the content will be appended to the end of the original file

x

Create write mode, create the file if it does not exist, and return the exception FileExistsError if it exists

rb

Open a file in binary format read-only, with the file pointer at the beginning of the file

wb

Open a file in binary format for overwriting, create if the file does not exist, or completely overwrite the source file if it exists

ab

Open a file in binary format for append writing, if the file exists, the pointer is at the end of the file, if not, create a new file for writing

r+

Open a file for reading and writing

w+

Open a file for reading and writing, create a file if it does not exist, and completely overwrite the source file if it exists

a+

Open a file for reading and writing, create a file if it does not exist, and the pointer at the end of the file if it exists

rt

Open a file in text format read-only, with the file pointer at the beginning of the file

wt

Open a file in text format for overwriting, create if the file does not exist, or completely overwrite the source file if it exists

Different modes are suitable for different scenarios, and you should choose according to the actual situation.

3.Python provides three file reading methods, read()、readline()、readlines(), among which, read()read all the contents of the file and read readlines()all the lines, these two methods are usually not recommended. When the file is very large, for example, you are asked to read a 20G file, the general computer memory may be 16G, and it is definitely impossible to read all of it into the memory. So we generally solve it by traversing the for loop, which is faster. The file can be processed line by line in the following format:

fo = open(fname, 'rt') #fname为文件名称或其绝对路径
for line in fo:
    print(line) #可以为处理一行数据的其他操作,这里以打印为例
fo.close()

4. The encoding methods of different files are different. The default value of Windows Simplified Chinese system is "GBK". When you need to read and write files using other encoding methods, you need to modify the encoding parameter, as shown in the following format.

fo = open('测试文件.txt', 'rt', encoding='utf-8') 
#有BOM格式的参数为'utf-8-sig'
for line in fo:
    print(line)
fo.close()

When the file is opened and read, its pointer is at the end of the file and cannot be read again. If you do other operations at this time, you need to seek(0)move the pointer to the beginning of the file, or reopen the file. seek()The method can be tell()used in conjunction with the method tell()to display the position of the file pointer, which is the number of bytes from the beginning of the file.

fo = open('测试文件.txt', 'rt')
for line in fo:
line = line.replace('\n', '')
print(line)
# 再读取一遍
for line in fo:
line = line.replace('\n', '')
print(line)
fo.close()

Although the program reads the file twice, but after the first read, the file pointer is already at the end of the file, and no content will be read again, so the result shown in the figure below will appear.

picture

5. The default newline character in the windows system is \r\n, while the newline character of Python is \n, in text mode, open()the default in the function, newline=Nonewhen reading a file, it will convert \n、\r、\r\nthe line ending with it to \n, when writing a file, all \nwill be converted into The system default newline character. If newline='', no conversion will be done when reading and writing files.

6. When Python reads and writes files, if only the file name is given, the system can only find the file in the same folder as the py program. For files in other locations, the absolute path of the file needs to be given, otherwise the file cannot be found, such as the absolute path below, when using an absolute path, since \ is an escape character, you need to use \\ instead of \.

f = open('D:\\python\\testfile.txt', 'rt')

picture


Long press or scan the QR code below to get  free Python open courses and hundreds of gigabytes of learning materials packaged by the big guys , including but not limited to Python e-books, tutorials, project orders, source code, cracked software, etc.

picture

Scan the QR code - get it for free

Recommended reading

Click  to read the original text  to learn more