Python Basics (bottom)

Source: Internet
Author: User

I. Read and write Files

1. Open file : "Open" (filename, mode). mode has the following modes, which are read-only (R) by default.

2. Writing files

f = open ("/home/test.txt","W")#F.write (s) writes s to the file, and then returns the number of characters written. num = F.write ("Come on baby!\n let's Go party")Print(num)#If you are writing non-string data, you need to first convert to a string:Value = ('Amy', 14,' China') s=Str (value) f.write (s) f.close ()

3. Reading files

f = open ("/home/test.txt","R") Str= F.read ()#f.read (size) reads a certain number of data, and when size is ignored or negative, all the contents of the file are read. str = F.readline ()#F.readline () reads a line with a newline character ' \ n '. When an empty string is returned, the description has been read to the last line. str = F.readlines ()#f.readlines () reads all rows, and if the optional parameter sizehint is set, the bytes of the specified length are read and the bytes are split by row. Print(str)#iterate over the file object and read each line forLineinchF:Print(Line, end="') F.close ()

4. Move the file location

F.tell () returns the current location of the file object, which is the number of bytes that are counted from the beginning of the file.

f.seek ()   Change the current position of the file, syntax: F.seek (offset, from_what)      from_what for 0 means move from the beginning of the file, 1 represents the current position, 2 represents the end of the file, the default is 0
Seek (x  , 0): Move x characters from start position, beginning of file first line character
  Seek (x,1): Indicates that the X-character is moved backwards from the current position
Seek (-x,2): represents moving forward x characters from the end of a file

f = open ('/home/test.txt'rb+') f.write (b')  0123456789abcdef') F.seek (5)     #f.read (1)      #  B ' 5 '#  move to the bottom of the file third byte f.read (1)     #  B ' d 'f.close ()

5. Pickle Module-Object serialization

A. Saving a data object to a file

Importpickledata1= {'a': [1, 2.0, 3, 4+6j],         'b': ('string', u'Unicode String'),         'C': None}list1= [1, 2, 3]list1.append (list1) F= Open ('DATA.PKL','WB') Pickle.dump (data1, F)#using protocol 0Pickle.dump (List1, F,-1)#use the highest protocolF.close ()

B. Refactoring a Python object from a file

Python Basics (bottom)

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.