Python Study Notes-day 5, file read/write and basic serialization, python Study Notes

Source: Internet
Author: User

Python Study Notes-day 5, file read/write and basic serialization, python Study Notes

When we use a large amount of data, we cannot write all the data into the program. We can write the data into the file and read or modify it when the program needs to be used.

Read/write files

When you open a file object in Read mode and use the built-in function open (), a file object is returned. The basic syntax format is as follows:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)    Open file and return a stream.  Raise IOError upon failure.open('C:\\***\\test.txt', mode)

The following are some parameters of mode. The default value is r read-only:

'R': open the file in read-only mode (default)
'W': open the file as a write and overwrite the existing file.
'X': if the file already exists, opening in this mode will cause an exception.
'A': open in write mode. If the object exists, append the object to the end.
'B': open the file in binary mode.
'T': open in text mode (default)
'+': Read/write mode
'U': supports common line breaks

Open (filename, w) is used only for writing unreadable files. If the file already exists, overwrite it. If the file does not exist, create a new file.

Close the file after it is opened. f. close ()

File object Method
F. close (): close the file
F. read (size =-1): read size characters from the file. When no size or negative value is given, read all the remaining characters and return them as strings.
F. readline (): Open in write mode. If the object exists, append the object to the end.
F. write (str): write the string str to a file.
F. writelines (seq): writes a string sequence seq to a file. seq should be an iteratable object that returns a string.
F. seek (offset, from): Move the file pointer in the file, from (0 indicates the start position of the file, 1 indicates the current position, offset byte)
F. tell (): returns the current location of the file

 

Pickle Module

The pickle module implements basic serialization and deserialization.

The process of changing variables from memory to storage or transmission is called serialization,

After serialization, The serialized content can be written to the disk or transmitted to another machine over the network.

In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling.

import picklepickle_file = open('C:\\***\\test.txt', 'wb')pickle.dump(obj, file[, protocol])

 

We use pickle. dump to serialize an object and then write it to file.

Then close the file and choose pickle_file.close ()

The object is written into the test.txt file for long-term storage.

 

If we want to use data in the test.txt file, we need to use the pickle. load () method to deserialize the object.

F = open('test.txt ', 'rb') Open the test.txt file pickle_file = pickle. load (f) # deserialize the object f. close ()

When the opened file is null or does not exist, the following error occurs: EOFError: Ran out of input. Check whether the input file name or path is incorrect.

In this case, the object f is deserialized from test.txt, satisfying our needs to retrieve data from the file.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.