Concept of documents
Computer files , It is a piece of data stored on a long-term storage device
Long term storage equipment includes : Hard disk 、U disc 、 mobile hard disk drive 、 Compact disc …
Role of documents
Save the data for a long time , Use when needed
How files are stored
In calculation , The file is saved on disk in binary form
Text files and binaries
text file :
You can use text compilation software to view
It's still binary in nature
for example :python Source program
Binary
The content saved is not for direct reading , But for other software
for example : Picture file 、 Audio file 、 Video file …
Binary files cannot be viewed with text compilation software
Operation of file
The routine of operating files in the computer is very fixed , There are three steps :
1. Open file
2. read 、 Writing documents
read Read the contents of the file into memory
Write Write the contents of memory to a file
3. Close file
Functions for manipulating files / Method
stay python In the operation file, you need to remember 1 A function and 3 A way
open The function is responsible for opening the file , And return the file object
read/write/close All three methods need to be called through a file object
read Method - - - - - Read the file
open The first parameter of the function is the file name to open ( File names are case sensitive )
If the file exists , Return the operation object
If the file doesn't exist , An exception will be thrown
read Method can read in and return all the contents of the file at once
close Method is responsible for closing the file
If you forget to close the file , It will cause system resource consumption , And it will affect subsequent access to files
Be careful : After method execution , Will move the file pointer to the end of the file
How to open a file
open The function defaults to Read only mode Issue documents , And return the file object
The grammar is as follows :
f = open(" file name ", " access ")
** Tips :** Frequent movement of file pointers , It will affect the efficiency of reading and writing files , More often than not, development is done with read-only 、 Just write To operate the file
Read the contents of the file by line
read Method will read all the contents of the file into memory at one time by default
If the file is too large , The memory consumption will be very serious
readline Method
readline Method can read one line at a time
After method execution , Will move the file pointer to the next line , Ready to read again
Example :
# Open file
file = open(" file name ")
while Ture:
# Read a line
text = file.readline()
# Judge whether the content is read
if not text:
break
# There is already a at the end of each read line "\n"
print(text, end="")
# Close file
file.close()
Copy file
Example : Small file copy
# First open two files , file 1 There are initial contents , file 2 Is a blank file
file1 = open(" file name 1")
file2 = open(" file name 2", "w")
# To operate
text = file1.read()
file2.write(text)
# Close file
file1.close()
file2.close()
Big file replication
If the source file is very, very large , It is not appropriate to read the source file at one time , So we can read it line by line 、 Write
# First open two files , file 1 There are initial contents , file 2 Is a blank file
file1 = open(" file name 1")
file2 = open(" file name 2", "w")
# To operate
while Ture:
text = file1.readline()
# Judge whether the content is read
if not text:
break
file2.write(text)
# Close file
file1.close()
file2.close()
file / Common management operations of directory
At terminal / Regular files can be executed in the file browser / Directory management operations
for example :
establish 、 rename 、 Delete 、 Change path …
stay python If you want to realize the above functions through the program , You need to import os modular
(ipython3)
File operations
Directory operation
The kind of graphic editing so
Unittest The framework is intr