1, python open file: built in function open
File operation: open (parameter 1, parameter 2, parameter 3) -- other parameters are generally not used. See the notes for details
Parameter 1: specify file
Parameter 2: open model
r: Read
a: Append write. If it does not exist, it will be automatically created
w: Overwrite write. If it does not exist, it will be created automatically
The following three are used to operate some videos, pictures and other files
Open the file in binary mode
rb
ab
wb
Parameter 3: encoding method (utf-8) – default gbk without specifying
1) Open files in the same directory and use relative paths
# Open a file and return the handle of an operation f = open(file="test.txt", mode="r", encoding="utf-8")# I like key word transmission, and I'm afraid of remembering the wrong position # print(f) # <_io.TextIOWrapper name='test.txt' mode='r' encoding='utf-8'> # File handle is an io stream, which can be regarded as a mouse and can operate on files
2) Open files in different directories and use absolute paths
# (right click the file [copy path] in pycharm) # Add "r" before the path to prevent string escape f = open(file=r"H:\python2022\test08\test.txt", mode="r", encoding="utf-8")
2, How python reads files
1. Read(): read all the contents of the file
# Open a file and return the handle of an operation f = open(file="test.txt", mode="r", encoding="utf-8") # Read content content=f.read() print(content) # Close the file and close it after open, otherwise the memory will be used all the time; Simple tests may not be added, but they will be added as a rule, which is similar to those that run all night f.close()
2. readline(): read one line at a time
f = open(file="test.txt", mode="r", encoding="utf-8") data1=f.readline() print(data1)
3. readlines (): read all the contents by line and put them into a list. Each line is an element in the list
f = open(file="test.txt", mode="r", encoding="utf-8") data2=f.readlines() print(data2)
Operation results:
['the first day of learning Python, happy, come on, artificial! \ n', 'the second day of learning Python, happy, come on, artificial! \ n', 'the third day of learning Python, happy, come on, artificial! \ n', 'the fourth day of learning Python, happy, come on, artificial! \ n', 'the fifth day of learning Python, happy, come on, artificial! \On the sixth day of learning Python, be happy, come on, be artificial! \On the 7th day of learning Python, be happy, come on, be artificial! \n’, ‘. . . \n’]
3, File write
a: Append write. If it does not exist, it will be automatically created
f = open(file="test.txt", mode="a", encoding="utf-8") f.write("python Abuse me a thousand times, I stay python As first seen") f.close()
File content after writing:
w: Overwrite write. If it does not exist, it will be created automatically
f = open(file="test.txt", mode="w", encoding="utf-8") f.write("python Abuse me a thousand times, I stay python As first seen") f.close()
File content after writing:
4, python reads files in other formats
It is usually used to read and write files (pictures, videos) in non text format
rb: read-only mode. Open it in binary mode. If the file does not exist, an error will be reported (indicating that the file cannot be found)
wb: write mode. Open it in binary mode, overwrite it (clear the original content). If the file does not exist, it will be created automatically
# Step 1: open the file f1 = open("002.gif", 'rb') f2 = open("003.gif", "wb") # Step 2: read the content and write a new file content = f1.read() f2.write(content) # Step 3: close the file f1.close() f2.close()
Run result: generate duplicate picture 003 gif
# Read Excel file (the running result is binary, which needs to be read by rb method) f = open('apicases.xlsx','rb') content = f.read() print(content) f.close()
Running result: b'PK\x03\x04\n\x00\x00\x00\x00\x00\x87N\xe2@\x00
ab: write mode (append). Open it in binary mode and write it additionally (write new content at the end of the original content of the file). If the file does not exist, it will be created automatically
5, Operating files through with
With: open the context manager that returns the file handle object (after executing the code statement of with, the file will be closed automatically)
with open(file="test.txt", mode="r", encoding="utf-8") as f: c = f.read() print(c) print(f)
Operation results:
python has abused me thousands of times, and I treat python as if I had seen it for the first time
<_io.TextIOWrapper name='test.txt' mode='r' encoding='utf-8'>
Using with is the same. with open is more commonly used. Don't worry about closing files
Note: the file cannot be operated after it is closed (an error is reported)