1. Open File
obj = file (' path and filename ', ' Open with ')
obj = open (' File path and filename ', ' Open with ') recommends using open
#r, W,a
obj = open (' log ')
Obj.tell () #取到指针的位置
Obj.seek (0) #跳到指定位置
PS: for open mode + sign only r+ is read-write, w+,a+ is equivalent to w a
Open mode RB opens in read-only binary binary
R+u to convert \ r \ n automatically to \ n
Open the file, the operation of the file will often forget to close the file, you can use with
With open (' Log ', ' R ') as obj:
The file is closed automatically once the code executed with it executes.
With support for opening multiple files after python2.7, rollback is also supported (e.g., configuration file modification error)
old.conf = R, new.conf W
With open (' Log1 ', ' R ') as Obj1,open (' log2 ', ' W ') as Obj2
For Lin Obj1:
New_line = Line.replace (' 10.6.7.8 ', ' 10.6.7.9 ')
Obj2.write (New_line)
The contents of the old configuration file can be read to the new configuration file, after the completion of the directory will generate two configuration files, check the new configuration file if there is no error
The new configuration file will work, and if it is wrong, use the original configuration file
Homework
Gracefully Modify the configuration file
Find
Add to
Delete
Python file operations