Python file manipulation and seek offset details

Source: Internet
Author: User
Tags one more line readline

First, the code in the Python file operation

This test is based on the Python 2.7.12 os:ubuntu16.04 pycharm Environment, and Win7 under 2.7.12;

First of all, the characters in the file occupies the number of bytes, this first look at the following experiment (WIN7) because Linux does not support GBK, this article does not speak Utf-8, GBK coding specific knowledge, interested can access http://www.ruanyifeng.com/blog/2007/ 10/ascii_unicode_and_utf-8.html or search for considerable information by yourself. This experiment is only about Python when using utf-8 and GBK encoding, the bytes occupied by the Chinese characters are different.

First look at the experiment:

650) this.width=650; "title=" 2.png "src=" https://s1.51cto.com/wyfs02/M02/9A/8F/ Wkiol1lxxvzqh-t4aacbokqpxtw923.png-wh_500x0-wm_3-wmp_4-s_3741509265.png "alt=" Wkiol1lxxvzqh-t4aacbokqpxtw923.png-wh_50 "/>


The code for the top right solution is as follows:

#-*-coding:utf-8-*-with Open ("Test2.txt", "W") as F:f.write ("Dong Hellow world!") F.write ("Python is a good language!\n") f.write ("Python is the best language in the world!")    With open ("Test2.txt", "R") as F:print (Len (F.read ())) F.seek (0) Print f.read (2) f.seek (0) Print F.read (3) F.tell ()

Save Run (F5) run results for the upper left corner of the figure appears length 78 and the characters "Dong" garbled again 3 bytes from the beginning to correctly display "dong"


The code for the right bottom solution is as follows:

#-*-CODING:GBK-*-with Open ("Test3.txt", "W") as F:f.write ("Dong Hellow world!") F.write ("Python is a good language!\n") f.write ("Python is the best language in the world!") With open ("Test3.txt", "R") as F:print (Len (F.read ())) F.seek (0) Print f.read (2) F.tell ()


Save Run (F5) run results to the bottom left corner of the figure appears 68 can correctly display "dong"


The contents of Test2.txt and Test3.txt are the same, but they are stored in files under different encodings!

It can be seen from the experiment that Chinese characters account for 3 bytes under UTF8 encoding, 2 bytes under GBK, and 1 bytes in English regardless of the code!



Second, Python file operation

To understand the above problems we can read the content by manipulating the file to prevent it from garbled.

1, Python file operation mainly through file (3 version removed), open (2, 3 versions have) function to achieve

The main modes of the Open function are described and compared:

Open with file exists file does not exist
r read Open file error
w write clear the original file contents Create and open a new file
a write only (append) keep the contents of the file, add new data to the end of the file Create and open a new file
r+ read/write error
w+ read/write purge file contents Create and open a new file
A + Write Keep the file contents and add new data to the end of the file Create and open a new file


Note: If this is a binary file operation, add a B to the above pattern, such as reading a binary file RB


2, the file Operation method experiment

F.write () #字符串写入文件

F.writelines #将一串字符串写入文件. The sequence can be any iterator object that generates a string, usually a list of strings

F.read ([size]) #默认读出文件中所有内容, you can specify a size (in bytes)

F.readline ([size]) #默认每次读取一行, a trailing newline character is retained in the string.

F.readlines ([size]) #默认将文件内容讲到列表中保存

F.flush () #将缓冲中的内容写入磁盘

F.tell () #显示当前文件的指针所在位置

F.close () #关闭打开的文件

F.seek () #对文件进行指针偏移操作, there are three modes,

Seek (0,0) by default moves to the beginning of a file or shorthand for seek (0)

Seek (x,1) represents a backward X (positive) byte from the current pointer position, and if X is a negative number, the current position is shifted forward by x bytes

Seek (x,2) represents the forward and trailing X (positive) bytes from the end of the file, and if x negative, moves the X bytes forward from the end


3. Detailed description of the document seek

The experiment code is as follows:

Create a file First File2.txt

#-*-Coding:utf-8-*-f = open ("File2.txt", ' w+ ') f.write ("I love Python\n") f.write ("Python is the most beautiful language in the world!") \ n ") f.write (" Python's most beautiful language in the world? ")

File2.txt content is as follows:

I Love Python
Python is the most beautiful language in the world!
Python's most beautiful language in the world


Working with the File2.txt

F = open ("File2.txt", ' R ') print (Len (F.read ()))       #文件总长度  89print ( F.tell ())            #读完文件, file pointer position 89f.seek (0,0)                 #偏移回文件头print (F.readline ())         #打印出文件中一行 (first line) Print f.tell ()              #显示文件指针现在的位置f. Seek (2,1)                 #从当前文件指针 (backward) offset 2 bytes print (F.readline ())       # Print one more line (should be 2 bytes less) print (F.tell ())            #显示   now   The pointer position F.seek ( -7,1)              # Offset forward from current position 2 bytes print (F.readline ())        #打印下一行print (F.tell ())             #显示   Now   pointer position F.Seek ( -9,2)               #从尾部向前偏移9个字符print ( F.tell ())            #显示   Now   pointer position print (F.readline ())        #打印出内容f. Seek (0)   #==> default is 0  et cetera seek (0,0) F.seek         #等同上面的seek ( -9,2)   Print from tail (f.readline ())

Results

650) this.width=650; "title=" 2.png "alt=" wkiom1lxwwar19haaaq7cnh4g2u611.png-wh_50 "src=" https://s5.51cto.com/ Wyfs02/m02/9a/8e/wkiom1lxwwar19haaaq7cnh4g2u611.png-wh_500x0-wm_3-wmp_4-s_3064912342.png "/>


This article is in the learning process of the experimental results, may be different versions of different platforms slightly error, if there is inappropriate, welcome to correct the exchange!

This article is from the "Learning, learning" blog, please be sure to keep this source http://dyc2005.blog.51cto.com/270872/1943682

Python file manipulation and seek offset details

Related Article

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.