Python can read, write, append, and replace (slightly more complex) the file without importing any modules.
Second, open the file:
1, f = open (' Text.txt ')
This mode is turned on and needs to be closed using f.colse () after the operation.
2. With open (' Text.txt ') as F:
This way, the system automatically shuts down and reclaims memory after running. Recommended for use.
Third, the file open mode:
Python operation of the file, the default is read-only, you need to specify a variety of modes to do the corresponding operation, mode list:
R
|
Open a file as read-only
|
r+
|
Read first, then write mode
|
Rb
|
Read-only open, picture, video and other files in binary format
|
rb+
|
Binary format, read first and then write
|
A
|
Open F.write () in append format, append on last line, file does not exist, create file.
|
A +
|
Open F.write () in read-write format, append on last line, file does not exist, create file. |
Ab
|
And a differ from, binary
|
ab+
|
And ab differ from, binary
|
W
|
Write mode create new file, overwrite original file, unreadable
|
w+
|
Write-read mode, write and reread, create a new file, overwrite the original file |
Wb
|
and W distinguished from, binary
|
wb+
|
Unlike WB, binary
|
Iv. Common methods of documentation:
F.read (num)
|
By default, the entire file is read, num can set the size of the data set (number of English characters)
|
F.readlines (num)
|
The default reads the entire file, and in the behavior delimiter, the return value is a list, one element per behavior
|
F.readline (num)
|
Save space by reading one row per run
|
F.write (str)
|
To write data to a file, str must be a string type.
|
F.tell ()
|
Show current pointer position, cannot use tell () when using next () method
|
F.seek (num)
|
Set the pointer position, the F.seek (0) file starts,
|
F.flush ()
|
Forces the memory data to be written to the file. The default program runs at the end before writing
|
Next (f)
|
Read file by line, F is an open file object, cannot be used in conjunction with tell (), General iterator Print (Read_file.tell ()) Oserror:telling position disabled by next () call |
F.close ()
|
Close open files, clear memory
|
|
|
Example 1:
#假设文件test. txt content as follows ' 0=0001=1002=2003=3004=4005=5006=6007=7008=800 ' #1 # the most space-saving, fastest way to read # read_ File.readline (), each time it runs, automatically reads the next line With open (' File_text1.text ', ' r ') as read_file: f = read_file.readline () while f: # in terms of judgment, None and False are false, the rest is true, welcome to supplement print (f, end= ') f = read_file.readline () # note end, because the ReadLine () read contains a newline character, print defaults to a blank line character, so an empty line appears #2# Other methods combined Operation With open (' File_text1.text ', ' r ') as read_file: f = read_file.read (5) print (f) # operation result is 0=000 print (Read_file.tell ()) # Current location is 5 read_file.seek # move the pointer to 22 print (Read_file.readlines ()) # ' =300\n ' starting at position 22 with a behavior element that makes up a list
Example 2:
Normally, when a file is closed properly, the memory file is written, and flush () is used to write the file immediately.
With the WITH statement, the file is automatically closed whenever you jump out of a statement block.
# input in with Inside D = {}with open (' file_text1.txt ', ' W ') as file: for i in range (): file.write (str (i ) + ' = ' + str (i) + ' 00\n ') s=input ('-----Stop! File not closed, cannot find file_text1.txt-----: ') # move input to with outside d = {}with open (' File_text1.txt ', ' W ') as file: for i in range (): file.write (str (i ) + ' = ' + str (i) + ' 00\n ') s=input ('-----Stop! The file closes properly, you can find file_text1.txt-----: ') # use Flush (), you can open ' file_text1.txt ', each line is immediately written to D = {}with open ( ' File_text1.txt ', ' W ') as file: for i in range (): file.write (str (i) + ' = ' + str (i) + ' 00\n ') file.flush () s=input ('-----Stop! There are documents, content-----: ')
Example 3:
To modify a file:
The file is only read, write, append, no modification method.
If you want to modify only read to memory modification then overwrite back.
For large files, this method accounts for memory and may cause a panic.
So, read by line, modified to deposit temporary files, is a good way!
Temp_file, source_file = ' temp_info.txt ', ' info.txt ' # Open two simultaneous files with open (Temp_file, ' W ', encoding= ' Utf-8 ') as Temp_file_ Write, open (source_file, ' R ', encoding= ' Utf-8 ') as File_read:file_read_line = File_read.readline () w Hile file_read_line:temp_file_write.write (file_read_line) file_read_line = File_read.readline () # and then put the above Temp_file and suorce_file to change position, write back to the good, is not found something? Consider it carefully:)
Python Basics: Actions for files