---- 04 ---- python entry document processing

First, a general introduction

  We operate on a computer, operation of the document in the final analysis, its essence is a request sent by the operating system, the user or application program file read and write operations to convert a particular command hard disk.

  As we all know, the data in memory is not permanent preservation. In computer hardware, the long-term preservation of data only computer's hard drive. Therefore, the essence of the operation is the operation of a file on the hard drive.

  Below, we will discuss how to implement specific operations on file with the python.

 


 

Second, the specific operation process

 First, the steps simple file can be divided into three steps:

  ① Open file and assigned to a variable, the file pointer in a similar language c.

  ② achieved through the operation of the variable additions and deletions to the data memory.

  ③ close the file.

 

File Open

grammar:

Open (File, MODE = ' R & lt ' )    
# simple
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
#完整的

 

 Parameter Description:

  • file: Required, file path (relative or absolute path)
  • mode: Optional, the file open mode
  • buffering: a buffer
  • encoding: UTF8 general use, the default mode is the coding system, the compiler is not
  • errors: error level
  • newline: distinguish line break
  • closefd: Incoming file parameter types

       mode parameter correspondence table

t   Text mode (default).
x Write mode, create a new file, if the file already exists it will error.
b Binary mode.
+ Open a file is updated (read and write).
The Universal wrap mode ( Python 3 does not support ).
r Open the file in read-only mode. Pointer file will be placed at the beginning of the file. This is the default mode.
rb Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as pictures and so on.
r+ Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+ Opens a file for reading and writing binary format. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as pictures and so on.
w Open a file for writing only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
wb Open a file for writing in binary format only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
w+ Open a file for reading and writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
wb+ Opens a file for reading and writing binary format. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures and so on.
a Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
from Open a file in binary format for additional. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
a+ Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. It would append mode when the file is opened. If the file does not exist, create a new file for reading and writing.
ab + Open a file in binary format for additional. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

 

Related Precautions:

1. Open the file modes are (default text mode):

  r, # [read-only mode the default mode, the file must exist, there is no exception is thrown]

  w, write-only mode # [unreadable; does not exist, create; there is then emptied the contents]

  a, the additional write mode # [unreadable; does not exist, create; there is only the additional content]

 

2. For non-text files, we can only use the b mode (without specifying the encoding when opening)

  "b" represents the manner in bytes, and the files are also stored in the form of bytes

   avi format to use this mode regardless of the text file character encoding, jgp format image files, video files

   When opened by b, the read byte is the type of content, but also a need to provide byte type writing, you can not specify the encoding

 

3. In the Windows environment, python newline is \ r \ n, newline under Linux to \ n 

 For the sake of consistency, the display is \ n-, to display as may be specified in a newline open () function in the open file = '' 

 


 

 

File Operations

Method 1: f.flush () function: to flush the buffer, the data buffer to write about to file immediately, while empty the buffer

Method 2: f.readable () action: whether the file readable

 

Method 3: f.writable () action: whether the file readable

 

Method 4: f.read () action: Reads the specified number of bytes from the file, or if the given negative is read all

Method 5: f.readline () function: the entire line is read from the file, including \ n-, the cursor moves to the head portion of the second row; if a non-negative parameter specifies the number of bytes specified size Returns

Method 6: f.readlines () action: Read all rows (until the end symbol EOF), and returns from the file list corresponding

 

Method 7: f.write () function: specified string is written to the file, can not see the content in the file before the file is closed before or refresh buffer

                  For text files, you need to write your own line breaks; when writing data to a binary file, you need to specify the encoding

                  如:  f.write('1111\n222\n'.encode('utf-8'))

Method 8: f.writelines () action: When writing to the file iterables, the write data in a binary file, needs to specify the encoding

                  如:  f.writelines([bytes('333\n',encoding='utf-8'),'444\n'.encode('utf-8')])

 

Method 9: f.truncate () function: From the first line of the file first byte is truncated, the file size bytes, open form of the document must be written;

                    After all bytes behind the truncated V is deleted, wherein at Widnows wrap system is two bytes in size. 

 Syntax: Parameters: size, truncate the file size bytes

fileObject.truncate( [ size ])

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

Move the cursor

seek ()  method is used to move the file pointer to read the specified location.

tell ()  method for the current cursor position clear


grammar:

fileObject.seek(offset[, whence])

 

parameter:

  • offset  - offset from the start, that is to represent the number of bytes required movement offset is negative if the first countdown starts from several.

  • whence: Alternatively, the default value is 0. Offset to define a parameter, which indicates a position offset from the beginning;

 

        0 代表从文件开头开始算起,1 代表从当前位置开始算起,2 代表从文件末尾算起。

 

 

返回值:

  如果操作成功,则返回新的文件位置,如果操作失败,则函数返回 -1。

 

 

实例:

>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)      # 移动到文件的第六个字节
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2)  # 移动到文件倒数第三个字节
13
>>> f.read(1)
b'd'

 

 

 


 

 

 

 

 

 

 

 


 

注意:

 

一、 打开一个文件包含两部分资源:操作系统级打开的文件+应用程序的变量。

在操作完毕一个文件时,必须把与该文件的这两部分资源一个不落地回收,回收方法为:
     f.close() #回收操作系统级打开的文件


二、 f=open(...)是由操作系统打开文件,如果没有为open指定编码,那么打开文件的默认编码由系统决定,
    
   操作系统会用自己的默认编码去打开文件,在windows下是 gbk,在Linux下是utf-8。

 

 

Guess you like

Origin www.cnblogs.com/zhaochuming/p/12313554.html