python read write txt file json file implementation method

  • 2020-05-17 05:41:22
  • OfStack

First step 1, open the file, and there are two functions to choose from: open() and file()

f = open('file.txt', 'w')
...
file.close()

(2). f = file (' file. json ', 'r)
...

file.close ()# remember to close the file at the end of opening it!

open() and file() are both built-in functions of Python, which return 1 file object. They have the same function and can be replaced arbitrarily. The syntax used is:

f = open(fileName, access_mode='r', buffering=-1)

The first parameter is the file name, 2, 3 has a default value, and the second parameter determines whether 'r' is read. Okay? Or is it written 'w'? Or open the file in some other way.

The opening method is as follows:

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 -- base 2 read; wb -- base 2 write; rb+ wb+ ab+ -- base 2 read and write

Example:


fp = open('C:\Users\MPC\Desktop\ instructions .txt')#  Opens as read by default 

fp = open('test.txt','w')#  Write mode open 

fp = open('data.json','a')# Append mode open 

Step 2, manipulate the file

Once you have a handle to the file object (fp in the example), you can manipulate the file.

The built-in operation methods of the file object are: input, output, in-file movement, and miscellaneous operations

1. The input

Functions: read (), readline (), readlines ()

Reads the contents of the file into a string variable/list

read() : reads the entire file into a string variable

Example:


fp = open('C:\Users\MPC\Desktop\ instructions .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 1 line in the open file and returns the entire line including the line terminator into the string variable

readline() also has an optional parameter size, with the default of -1, which means stop at the end of the line

readlines() : reads the entire file and returns a list of strings, with each element in the list being a string representing 1 line

Example:


fp = open('C:\Users\MPC\Desktop\ instructions .txt')

lines = fp.readlines()

for line in lines:

...

fp.close()

Or line 2 and 3 for short: for line in fp.readlines() :

After python2.3 with the introduction of iterators and file iterators (that is, file objects become their own iterators),

There is a more efficient implementation of the above example:


fp = open('C:\Users\MPC\Desktop\ instructions .txt')

for line in fp:

...

fp.close()

This method is recommended!

2. The output

Functions: write(), writelines()

Output the string/list to a 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 list of strings to a file. Note that line endings are not automatically added. You must manually add line endings at the end of each line if necessary.

What does that mean? Look at the case:


>>>s= [' hello ',' dude ']

>>>f= open('test.txt','w')

>>>f.writelines(s)

>>>f.close()

>>>s= [' hello \n',' dude ']

>>>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. Move within the file

Function: seek () tell ()

seek() : move the file read pointer to the specified location

tell(): returns the location of the file read pointer

Three modes of seek() :

(1) f.seek (p,0) moves when the p byte of the file is in absolute position

(2) f.seek (p,1) moves to p bytes after the current position

(3) f.seek(p,2) moves to p bytes relative to the end of the article


Related articles: