Get file handle: File = open (filepath,mode,encoding) opens files under FilePath path in mode mode, and coding encoding (where mode can be r,w,a, read and write r+ (common), write w+ (w+ Open File and W mode open the file will overwrite the original file content, so not commonly used), append read a+;rb,wb,ab) Read content: Read () >>> read all the contents of the file Note: Read can add parameters to specify the number of reads ReadLine () >>> reads a line of content into the file: write ("content") >>> write to file close file handle: Close () Gets the location of the file cursor: Tell () Move cursor: Seek (index) >>> move file cursor to index position empty buffer: flush () >>> write data in buffer to file truncated text: Truncate (start) >> > Truncate (delete) article content from start position with statement:
With
open (' Log1 ') as Obj1,open (' log2 ') as Obj2:...........................pass ..................................
manipulating files through the WITH statement the Python interpreter automatically closes files after the With statement ends
Note:
- Writing a string in binary to a file requires encoding a string such as Str.encode ("Utf-8")
- Using a For loop file, each time the loop reads a row in the file
Python path-file operations