First step, open the file, there are two functions to choose from: Open () and file ()
①. f = open (' file.txt ', ' W ')
...
File.close ()
②. f = file (' File.json ', ' R ')
...
File.close () #记得打开文件时最后不要忘记关闭!
Both open () and file () are Python built-in functions that return a file object with the same functionality and can be replaced arbitrarily. Use the syntax for:
f = open (FileName, access_mode= ' R ', Buffering=-1)
The 1th parameter is the file name, the 2,3 parameter has a default value, parameter 2 determines the way to read ' R '? Or is it written in the way ' W '? Or another way to open the file.
The ways to open them are:
r--read; w--write a--append, start with EOF, write at end of file
r+ w+ a+--are open in both read and write mode
rb--binary read; wb--binary write; rb+ wb+ ab+--binary Read and write
Example:
fp = open (' C:\Users\MPC\Desktop\ description. txt ') # Opens FP = open (' Test.txt ', ' W ') By default = Open (' Data.json ', ' a ') in the way it is written #追加方式打开
The second step, the operation of the file
When you get the handle to the file object (such as the FP in the example), you can manipulate the file.
The built-in methods for file objects are: input, output, move within files, and miscellaneous operations
1. Enter
Function: Read (), ReadLine (), ReadLines ()
Read the contents of a file into a string variable/list
Read (): reads the entire file into a string variable
Example:
fp = open (' C:\Users\MPC\Desktop\ description. txt ') All_file = Fp.read ()
Read () has an optional size parameter, which defaults to-1, indicating that the file will be read to the end (EOF)
ReadLine (): reads a line from the open file, and then returns the entire line including the line terminator to the string variable
ReadLine () also has an optional parameter of size, default-1, which indicates that the read-to-line terminator stops
ReadLines (): reads the entire file, returns a list of strings, each element in the list is a string that represents a row
Example:
fp = open (' C:\Users\MPC\Desktop\ description. txt ') lines = Fp.readlines () for line in Lines:...fp.close ()
or the 2nd 3 line abbreviated as: For lines in Fp.readlines ():
After python2.3, the introduction of iterators and file iterations (that is, the file objects became their own iterators)
The above example has a more efficient way of implementing:
fp = open (' C:\Users\MPC\Desktop\ description. txt ') for line in Fp:...fp.close ()
It is recommended to use this method!
2. Output
Function: Write (), Writelines ()
outputting a string/list to a file
Write (): Output a string to a file
>>>f= open (' Test.txt ', ' W ') >>>f.write (' helloworld! ') >>>f.close () >>>f= open (' Test1.txt ', ' W ') >>>f.write (' welcome\nto\n china! ') >>>f.close () >>>f= open (' Test1.txt ', ' W ') >>>f.write (' welcome\nto\n china! ') >>>f.close ()
Writelines (): Writes a list of strings to the file, notes that the line terminator is not automatically added, and, if necessary, must manually include a line terminator at the end of each line.
What do you mean? Look at the following example:
>>>s= [' Hello ', ' Buddy ']>>>f= open (' Test.txt ', ' W ') >>>f.writelines (s) >>>f.close () >>>s= [' Hello \ n ', ' Buddy ']>>>f= open (' Test.txt ', ' W ') >>>f.writelines (s) >>>f.close () >>>f = open (R ' I:\python\test.txt ', ' W ') >>>f.write (' First line\n ') >>>f.write (' Second line \ n ') >>>f.write (' third line\n ') >>>f.close () >>>lines = List (open (R ' I:\python\test.txt ')) >>>lines[' firstline\n ', ' Second line\n ', ' third line\n ']>>>first,second,third = open (R ' I:\python\ Test.txt ') >>>first ' firstline\n ' >>>second ' secondline\n ' >>>third ' Thirdline\n '
3. Moving within files
Function: Seek () tell ()
Seek (): Move the file to read the pointer to the established location
Tell (): Returns the location of the file read pointer
Three modes of Seek ():
(1) F.seek (p,0) Move when the file is at p Byte, absolute position
(2) F.seek (p,1) moves to p bytes relative to the current position
(3) F.seek (p,2) moves to p bytes after the end of the article
The above is a small series for everyone to bring the Python read and write TXT file JSON file implementation method of all content, I hope you support the script home ~