Python reads and writes txt files in json files,
The first step is to open the file. There are two functions available: open () and file ()
①.F = open('file.txt ', 'w ')
...
File. close ()
②.F = file ('file. json', 'R ')
...
File. close () # Remember not to close it when opening the file!
Both open () and file () are Python built-in functions that return a file object with the same functions and can be replaced at will. Syntax:
F = open (fileName, access_mode = 'R', buffering =-1)
The first parameter is the file name, and the second and second parameters have default values. The second parameter determines the read method 'R '? Or is the write method 'W '? Or open the file in other ways.
The following methods are available:
R -- read; w -- write; a -- append, write from EOF, that is, write at the end of the file
R + w + a + -- all open in read/write mode
Rb -- binary read; wb -- binary write; rb + wb + AB + -- binary read/write
Example:
Fp = open ('C: \ Users \ MCM \ Desktop \ description .txt ') # open fp = open('test.txt', 'w' by default in Read mode ') # open fp = open ('data. json', 'A') # enable append
Step 2: Operate the file
After obtaining the file object handle (such as fp in the example), you can operate the file.
Built-in operations on file objects include: input, output, file movement, and miscellaneous operations.
1. Input
Functions: read (), readline (), readlines ()
Read the content of the file into a string variable/List
Read (): read the entire file to the string variable.
Example:
Fp = open ('C: \ Users \ MCM \ Desktop \ description .txt ') all_file = fp. read ()
Read () has an optional size parameter. The default value is-1, indicating that the file will be read to the end (EOF)
Readline (): Read a row in the open file, and then return the entire row including the row Terminator to the string variable.
Readline () also has an optional parameter size. The default value is-1, indicating that the read-to-row Terminator is stopped.
Readlines (): reads the entire file and returns a string list. Each element in the list is a string representing a row.
Example:
Fp = open ('C: \ Users \ MCM \ Desktop \ .txt ') lines = fp. readlines () for line in lines:... fp. close ()
Or the 3 rows in 2nd are abbreviated as: for line in fp. readlines ():
Since the introduction of the iterator and file iteration (that is, the file object becomes their own iterator) after python2.3,
In the above example, there is a more efficient implementation method:
Fp = open ('C: \ Users \ MCM \ Desktop \ description .txt ') for line in fp:... fp. close ()
This method is recommended!
2. Output
Function: write (), writelines ()
Output string/list to file
Write (): outputs 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 the string list to a file. Note that the row Terminator is not automatically added. If necessary, you must manually add the row terminator at the end of each row.
What does it mean? See the following example:
>>> S = [' ', '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. File Movement
Function: seek () tell ()
Seek (): Move the File Read pointer to the specified position
Tell (): returns the position of the object read pointer.
Three Modes of seek:
(1) f. seek (p, 0) moves the absolute position of the object at the p-byte.
(2) f. seek (p, 1) Move p bytes relative to the current position
(3) f. seek (p, 2) Move to the p byte after the end of the relative article
The above is all the implementation methods for reading and writing txt files in python ~