[File] read mode
Object file: file read and write file operations performed by the object.
Python2 ----- File
Python3 ----- TextIOWrapper
Open the file:
Built-in function open (file, mode) --- open files, getting a file object.
You must have a file file path (path, file name, file format) or an error.
fo = open (fileDir) ----- readonly
Writing path : ① absolute path starting from the root directory -----
② ------ relative path . / Current position .. / on a
fileDir = 'C:/test.txt'
fileDir = 'C:\\test.txt'
fileDir = r'C: \ test.txt '--- unescaping
Read mode: If the file does not exist will complain!
fo = open(dileDir, 'r') <==> fo = open(fileDir)
fo.tell () -------------- 0 from the start position of the display pointer
fo.read (2) ------------ read two characters
fo.read () -------------- read the entire contents
Wrapping the file length is 2 \ n 'a \ nb' ----- is a string
fo.close () --- close the file
Move the file pointer position : seek ()
0 mode : --- absolute position mode fo.seek (1,0) starting from 0 in general like txt, log r
Premise: python3 certain in 'rb' mode ---- --- non-text files in binary mode (pictures)
Mode 1 : the current position starts moving fo.seek (number of bits by, mode 1) ----- such as: fo.seek (-1, 1) rb
Mobile-digit positive number: move back negative: to move forward
Mode 2 : --- the end position of the start fo.seek (-1,2) rb
Mobile-digit positive number: move back negative: to move forward
Code examples:
fileDir = 'C:/test.txt'
fo = open (fileDir, 'rb') # Open File
print ( 'before reading', fo.tell ()) # display the current position of the cursor
print (fo.read (2)) # read two characters
print ( 'reading', fo.tell ()) # display the current cursor position again
fo.seek (2,2) # move move backward two
print ( 'post-movement', fo.tell ()) # again display the current position of the pointer
readline: read a line
1 - This method returns a print (type (fo.readline ())) --- <class 'str'>
2 - the file pointer will shift accordingly
readlines: read all the lines
1 - This method returns the print (type (fo.readlines ())) --- <class 'list'>
Difference: ① fo.read () -------- return str
② fo.readlines () -------- return is a list
2 -. Fo.read () splitlines () --- Returns the list and remove newline
=================================================================================
[File] write mode:
Write mode features: You can create a new file, if the file does not exist a new file is created;
If the write mode to open a file, all content will be cleared.
1 - fo = open(fileDir, 'w')
2 - If the file exists in the path will be cleared ---
3 - If the file does not exist in this path will create a new ----
4 - In pycharm Inside, you implemented fo.write ( '123') ----- can be written directly into
5 - fo.write ( '123') --- ---- returns the value written character length
6 - fo.flush () to write files to force
7 - fo.close () will be forced to close the file written to a file
Appending a: just to append the contents of the file and open the file
with open way:
1 - with open(fileDir) as rFile: ------等价于--------- rF = open(fileDir)
2 - may be omitted fo.close ()
3 - multiple files
the Seek: 1 - (1, mode) mode ---- --- 0 absolute position, starting from 0
2 - (1 mode) mode ---- --- 1 current position to start
3 - (1, Mode) ---- --- end position of the second mode, the start
example:
fileDir2 = open('C:/text.txt', 'w')
fileDir2.write ( '123456') # must be str
# If the write time, the need for line, how should I do?
fileDir2.write('01-test'+'\n')
fileDir2.write('02-test')
fileDir2.flush () # write
fileDir2.close () # close will also save