Codecs module: Handle garbled problem during file operation.
F=codecs.open (' Fxh.txt ', ' r+ ') #打开文件
Open mode:
R, W, A, B, r+, w+ ....
Common methods:
Read () reads the contents of the file
Write () writes the content, must pass in the string
WriteLine () write content, must pass in a sequence
Next line after next () cursor
Seek () sets the position of the cursor
Tell () to see where the cursor is
Flush () flush to disk
Name File name
Closed whether the shutdown is off or true
To open a file using with: (a file that is opened with the with does not have to be closed manually)
Eg: Prints a specific line of the file.
With Codecs.open (' fxh.txt ', ' r+ ') as FD:
#text =fd.read ()
For Line,value in Enumerate (FD):
if line = = 2-1:
Print (value)
The above implementations can also be implemented using a single module:
Import Linecache
Test=linecache.getline (' Fxh.txt ', 2)
Print (test)
Python file operations