Python read/write files are widely used in computer languages. If you want to know the applications, the following articles will introduce you in detail, it will help you in the future learning process. Next we will introduce its application in detail.
1. Open a file
Python read/write files are widely used in computer languages. If you want to know the applications, the following articles will introduce you in detail, it will help you in the future learning process. Next we will introduce its application in detail.
The Code is as follows:
- f = open("d:\test.txt", "w")
Note:
The first parameter is the file name, including the path, and the second parameter is the mode in which the file is enabled.
'R': Read-Only default. If the object does not exist, an error is thrown)
'W': automatically creates a file if the file does not exist)
'A': append to the end of the file
'R + ': read/write
To open a file in binary mode, add the character "B" after the mode, such as "rb" wb ".
Ii. Read content
- f.read(size)
The size parameter indicates the number of reads, which can be omitted. If the size parameter is omitted, all contents of the file are read.
- f.readline()
Reads the content of a row of a file.
- f.readlines()
Read all rows into the array [line1, line2,... lineN]. This method is often used to avoid loading all file content into the memory, so as to improve efficiency.
3. Writing files
- f.write(string)
Write a string to the file. If the write is complete, add "\ n" to the string, and then f. close () to close the file.
Iv. File Content locating
- f.read()
After reading the file, the file pointer reaches the end of the file. read () will find that the read is empty. If you want to read all the content again, you must move the positioning pointer to the file to start:
- f.seek(0)
The format of this function is as follows in bytes ):
- f.seek(offset, from_what)
From_what indicates the start reading position, and offset indicates moving a certain amount of distance from from_what. For example, f. seek (10, 3) indicates positioning the third character and moving the last 10 characters. When the from_what value is 0, it indicates the start of the file. It can also be omitted. The default value is 0, indicating the start of the file. The following is
- f = open('/tmp/workfile', 'r+')
- f.write('0123456789abcdef')
- f.seek(5) # Go to the 6th byte in the file
- f.read(1)
- '5'
- f.seek (-3, 2) # Go to the 3rd byte before the end
- f.read(1)
- 'd'
-
5. Close files and release resources
After file operations are completed, you must close the file f. close () to release resources for other programs.
Python read/write files are widely used in computer languages. If you want to know the applications, the following articles will introduce you in detail, it will help you in the future learning process. Next we will introduce its application in detail.
- How does Python IDE adapt to the current turbulent market
- How to unpack Python sequence tutorial 3G and IPTV are the Development Trend
- Python string processing with flexibility as its biggest advantage
- How to Use python to process text in vim
- Install PyDev in Python plug-in