Python has a built-in open() method for reading and writing files. Using the open() method to manipulate a file is like putting an elephant into a refrigerator. There are three steps: one is to open the file, two is to manipulate the file, and three is to close the file.

The return value of the open() method is a file object that can be assigned to a variable (file handle). Its basic syntax format is:

f = open(filename, mode)
Copy the code

In Python, all objects with read and write methods can be classified as file. All objects of type file can be opened using the open method, closed by the close method, and managed by the with context manager. This is one of Python’s design philosophies.

  • Filename: a string value containing the name of the file you want to access, usually a file path.
  • Mode: The mode in which the file is opened. There are many modes. The default mode is read-onlyr.

A simple example:

F = open("/ TMP /foo.txt", "w") f.write("Python is a very good language. \n I love Python!! \n") # close open file f.close()Copy the code

Description:

There are three ways to write paths in Python: 1.\ 2.r” 3. ‘/’ (recommended).

1. Open the mode

model operation instructions
r read-only The default mode, if the file does not exist, the error is reported, and the file is read normally.
w Just write If the file does not exist, create a new file and write it. If yes, clear the file before writing it.
a additional If the file does not exist, create a new file and write it. If so, append to the end of the file.
x new If the file exists, an error is reported. If the file does not exist, a new file is created and the content is written. It is safer than w mode.
b Binary mode For example, rb, WB, and AB operate bytes data and can be used to operate non-text files.
+ Read/write mode Like r plus, w plus, a plus

1.2. Mode B

Binary mode, usually used to read binary files such as pictures and videos. Note that it reads and writes as bytes, so it gets a bytes object instead of a string. During this read-write process, you need to specify the encoding format. Be careful about the data type you pass in when using the pattern with b. Make sure it is bytes.

S =' this is a test' b = bytes(s,encoding='utf-8') f = open('test.txt','w') f.write(s) ## ##------------------------------------------------- s = 'this is a test' b = bytes(s,encoding='utf-8') f = Open ('test.txt','wb') ## A bytes-like object is required, not 'STR' ## But you gave a string # # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - s = 'this is a test' b = bytes (s, encoding = "utf-8") = f Open ('test.txt','wb') ##Copy the code

1.3 + mode (not used much)

  • For W + mode, readable, writable, file does not exist first create, will be overwritten, before reading and writing will empty the contents of the file, do not use!
  • In a+ mode, the file can be read and written. The file is not created first and will not be overwritten. The file can only be written at the end of the file forever.
  • For R + mode, it is readable and writable, and an error will be reported even if the file does not exist. Write operations will be overwritten, and more operations can be achieved with seek() and tell() methods.

1.4 Coding problems

To read a file that is not UTF-8, pass the encoding argument to the open() function. For example, to read a file that is GBK encoding:

>>> f = open('gbk.txt', 'r', encoding='gbk')
>>> f.read()
'GBK'
Copy the code

A UnicodeDecodeError exception may be thrown when some files are not properly encoded, indicating that there may be some illegally encoded characters in the file. In this case, you can provide the errors argument, which indicates what to do if an encoding error is encountered.

>>> f = open('gbk.txt', 'r', encoding='gbk', errors='ignore')
Copy the code

2. File object operations

Every time we open a file with the open method, we return a file object. This object has a number of built-in operations. Let’s assume that an f file object has been opened.

2.1, f.r ead (size)

Read data of a certain size and return it as a string or byte object. Size is an optional numeric parameter that specifies the amount of data to be read. When size is ignored or negative, all the contents of the file are read and returned.

f = open("1.txt", "r")

str = f.read()
print(str)

f.close()
Copy the code

If the file size is large, do not use the read() method to read the file into memory once, but read(512) bit by bit.

2.2, f.r eadline ()

Reads a line from a file. The newline character is ‘\n’. If an empty string is returned, the last line has been read. In this method, you usually read a line, process a line, and you can’t go back, you can only go forward, and you can’t read a line again.

f = open("1.txt", "r")
str = f.readline()
print(str)
f.close()
Copy the code

2.3, f.r eadlines ()

Read all the lines in the file, line by line, into a list, one at a time as the elements of the list, and return the list. The readlines method reads the file into memory all at once, so there is some risk. But it has the advantage that each line is stored in a list and can be accessed at will.

f = open("1.txt", "r")
a = f.readlines()
print(a)
f.close()
Copy the code

2.4. Traversal files

In fact, more often than not, we use the file object as an iterator.

F = open("1.txt", "r") for line in f: print(line, end= "")Copy the code

This method is simple and does not require you to read the file at once, but it also does not provide a good control. Like the readline method, you can only go forward, not back.

Several different ways of reading and traversing a file are compared: if the file is small, read() is the most convenient to read once; It is safer to call read(size) repeatedly if you are unsure of the file size; If it is a configuration file, it is most convenient to call readlines(). In general, it's better and faster to use a for loop.

2.5, f.w rite ()

Writes string or bytes data to a file. The write() action can be repeated many times, but it is an in-memory operation and is not immediately written back to the hard disk. It is not until close() is executed that all writes are reflected to the hard disk. In the process, if you want to save the changes in memory to hard disk immediately, you can use the f.lush () method, but this can cause data inconsistency.

F = open("/ TMP /foo.txt", "w") f.write("Python is a very good language. \n I love Python!! \n") # close open file f.close()Copy the code

2.6 f.t, ell ()

Returns the current position of the file read/write pointer in bytes from the beginning of the file. It’s important to note that it’s bytes, not characters.

2.7, f.s eek ()

To change the position of the position pointer, use the f.see (offset, from_what) method. Seek () is often used in conjunction with the tell() method.

  • from_whatIf the value is 0, it starts from the beginning of the file. If the value is 1, it starts from the current position of the file read/write pointer. If the value is 2, it starts from the end of the file.
  • Offset: indicates the offset.
  • Seek (x,0) : Move x characters from the starting position, i.e. the first character of the first line of the file
  • Seek (x,1) : moves x characters back from the current position
  • Seek (-x,2) : Moves x characters forward from the end of the file

Here’s an example:

>>> f = open("d:\1.txt", "rb+")
>>> f.write(b"1232312adsfalafds")
17
>>> f.tell()
17
>>> f.seek(5)
5
>>> f.read(1)
b'1'
>>> f.seek(-3, 2)
14
>>> f.read(1)
b'f'
Copy the code

2. Close the file

2.1, f. lose ()

Close the file object. When a file is processed, call f.close() to close the file and free up system resources. If you try to call the file object again after the file is closed, an exception will be thrown. The consequences of forgetting to call close() are that only part of the data may be written to disk and the rest lost, or worse. That means don’t forget to close the refrigerator door after the elephant is stuffed in.

3. Copy files

Enter the name of the file, and the program automatically completes the backup of the file.

Import OS file_name = input(' please enter a file path: ') if os.path.isfile(file_name): old_file = open(file_name, 'rb') names = os.path.splitext(file_name) new_file_name = names[0] + '.bak' + names[1] new_file = open(new_file_name, While True: content = old_file.read(1024) # New_file. write(content) if not content Break new_file.close() old_file.close() else: print(' the file you typed does not exist ')Copy the code

4, with keyword

The with keyword is used in Python’s context manager mechanism. In order to prevent such as open the file open method in the operation process of exceptions or errors, or finally forget to execute the close method, the file closes abnormally and may lead to file leakage, damage problems. Python provides the with context manager mechanism to ensure that files are closed properly. Under its management, there is no need to write close statements. Notice the indentation.

with open('test.txt', 'w') as f: f.write('Hello, world! ')Copy the code

With supports opening multiple files at the same time:

with open('log1') as obj1, open('log2','w') as obj2:
    s=obj1.read()
    obj2.write(s)
Copy the code