Original link: https://chegva.com/5476.html
◎Knowledge points
-
open a file
-
read file
◎Script practice
▽ Open file
""" Storing data in a file is one way of storing data. How to write data to a file and read the data written to the file? Before a file can be read or written, the file must be opened. The built-in function open() is used to open a file, The first parameter is the path to the file, which must be specified. Either an absolute path or a relative path can be specified. The second parameter is the opening method of the file, the default value is 'r', which means to open in read-only mode. Other parameters are with default values, please refer to the official documentation. The return value of the function is a file object, through which the file can be manipulated, such as: reading the file, writing the file, closing the file, etc. """ # file = open('myfile.txt') # file = open('myfile.txt', 'r') # file = open('myfile2.txt') # file = open('myfile.txt', 'w') # file = open('myfile2.txt', 'w') # file = open('myfile.txt', 'a') # file = open('myfile2.txt', 'a') # file = open('myfile.txt', 'x') # file = open('myfile2.txt', 'x') # file = open('myfile.txt', 'r+') # file = open('myfile2.txt', 'r+')
▽ Read file
""" Before reading a file, the file must be opened. The built-in function open() can be called and a file can be opened read-only or read-write. The returned file object has three methods for reading the file: 1. read([size]) If no arguments are passed, read to the end of the file. If a parameter is passed in, size is used to specify the number of bytes, When the specified number of bytes is less than the number of bytes read to the end of the file, read the specified number of bytes; When the specified number of bytes is greater than the number of bytes read to the end of the file, or when the specified number of bytes is less than 0, read to the end of the file. If the file is large, calling read() to read the entire file at once will cause a large memory footprint, so it is best to call read(size) multiple times. The specified size should not exceed the default buffer size, otherwise, the specified number of bytes may not be read. Through the module io in the standard library The attribute DEFAULT_BUFFER_SIZE can view the default buffer size. """ """ >>> import io >>> io.DEFAULT_BUFFER_SIZE 8192 >>> file = open('myfile.txt') >>> file.read() '1234567890\nabcdefghijklmn\nopqrstuvwxyz' >>> file.close() # The file must be closed after use >>> file = open('myfile.txt') >>> file.read(12) '1234567890\na' >>> file.read(30) # Continue reading 'bcdefghijklmn\nopqrstuvwxyz' because the file is not closed >>> file.close() >>> file = open('myfile.txt') >>> file.read(12) '1234567890\na' >>> file.read(-5) 'bcdefghijklmn\nopqrstuvwxyz' >>> file.close() """ """ 2. readline([size]) If no arguments are passed, read to the end of the line. If a parameter is passed in, size is used to specify the number of bytes, When the specified number of bytes is less than the number of bytes read to the end of the line, read the specified number of bytes; When the specified number of bytes is greater than the number of bytes read to the end of the line, or when the specified number of bytes is less than 0, read to the end of the line. In short, read up to the end of the line. """ """ >>> file = open('myfile.txt') >>> file.readline() '1234567890\n' >>> file.readline(7) 'abcdefg' >>> file.readline(10) 'hijklmn\n' >>> file.readline(3) 'opq' >>> file.readline(-5) 'rstuvwxyz' >>> file.close() """ """ 3. readlines([size]) If no arguments are passed, read to the end of the file and return a list of each line. If a parameter is passed in, size is used to specify the number of bytes, When the specified number of bytes is less than the number of bytes read to the end of the file, read until the end of the line where the last character is located; When the specified number of bytes is greater than the number of bytes read to the end of the file, or when the specified number of bytes is less than 0, read to the end of the file. If the file is large, calling readlines() to read the entire file at once will result in a large memory footprint, so it is best to call readlines(size) multiple times. The specified size does not exceed the default buffer size. """ """ >>> file = open('myfile.txt') >>> file.readlines() ['1234567890\n', 'abcdefghijklmn\n', 'opqrstuvwxyz'] >>> file.close() >>> file = open('myfile.txt') >>> file.readlines(8) ['1234567890\n'] >>> file.readlines(50) ['abcdefghijklmn\n', 'opqrstuvwxyz'] >>> file.close() >>> file = open('myfile.txt') >>> file.readlines(8) ['1234567890\n'] >>> file.readlines(-5) ['abcdefghijklmn\n', 'opqrstuvwxyz'] >>> file.close() """
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/file_operations.py
This article is reprinted from: https://chegva.com/5476.html
This site is for inclusion only, and the copyright belongs to the original author.