python learning record 8 ---------- file File Operations

---------------------------- ------------------- copy rookie tutorial ---------

Links: https://www.runoob.com/python3/python3-file-methods.html

-------------------------------------------------------------------------

The file operation requires three steps: 1, file.open (); 2, file operations; 3, file.close ()

open () method

Python open () method is used to open a file and returns a file object in the file processing is required to use this function, if the file can not be opened, will throw OSError.

Note: Use open () method must ensure that the closed file object that calls the close () method.

open () function accepts two parameters are used in the form of: a file name (file) and a mode (mode).

open(file, mode='r')

The complete syntax is:

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
  • errors: error level
  • newline: distinguish line break
  • closefd: Incoming file parameter types
  • opener:

mode parameters are:

mode description
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.

The default is text mode, if you want to open in binary mode, plus  b.

Object file

file object using open function to create, the following table lists the file objects commonly used functions:

No. Method and Description
1

file.close()

Close the file. After closing the file can not read and write operations.

2

file.flush()

Internal refresh buffer files directly to the internal buffer of data written to the file immediately, rather than passively waiting for the output buffer written.

3

file.fileno()

Returns an integer file descriptor (file descriptor FD integer), it can be used in a method as read some of the underlying operating os module.

4

file.isatty()

If the file is connected to a terminal device returns True, otherwise False.

5

file.next()

Python 3 does not support the File object next () method.

Returns the file the next line.

6

file.read([size])

Reading the specified number of bytes from the file, it is not given, or if the reading of all negative.

7

file.readline([size])

Read the entire line, including the "\ n" character.

8

file.readlines([sizeint])

Read all rows and returns a list, if a given sizeint> 0, returns the sum of approximately sizeint byte line, the actual read value may be larger than the sizeint, because of the need to fill the buffer.

9

file.seek(offset[, whence])

Set the current position of the file

10

file.tell()

Returns the file the current cursor position.

11

file.truncate([size])

The first line of the first character from the beginning of the file is truncated, file size characters, no size represents a cut from the current location; all the characters will be deleted after the cut, which wrap under Widnows system represents two characters in size.

12

file.write(str)

The string to the file, returns the written character length.

13

file.writelines(sequence)

Write a list of strings to the file sequence, if necessary wrap will have to add their own line breaks each line.

Traversing File All contents: for loop directly with file objects, not f.readlines ()

1 f = open('helloworld','r+',encoding='utf8') #打开文件
2 num = 0 
3 for i in f : #for循环内部将f对象做成一个迭代器,用一行,去一行
4     num +=1
5     if num ==2: #在第二行后面加。
6         i = ''.join((i.strip(),''))
7     print(i.strip())
8 f.close()#关闭文件

 返回光标位置以及设置光标位置:用于文件断点续传

1 f = open('helloworld','r+',encoding='utf8') #打开文件
2 num = 0
3 print(f.tell())#当前光标为0
4 f.read(10)#读光标后10个字符
5 print(f.tell())#,如果都为英文字符,则返回11,而中文字符是占个光标位
6 f.close()

 

Guess you like

Origin www.cnblogs.com/fangxiaosheng/p/11609406.html