25 reads the configuration file

https://blog.csdn.net/jiede1/article/details/79064780

1. Support Configuration File Format
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

  

Sections of a first (such as [the DEFAULT]) cause, then following a similar "name = value" in the form of key-value pairs. In fact, then "name: value" form is also available.

2. API Functions

-read (filename) reads the contents of the file directly
-sections () to get all of the section, and returns a list of
-options (section) get all the option of the section of
-items (section) to get all the key section of
- gET (section, option) values obtained in the option section, and return a string type
-getint (section, option) values obtained in the option section, and return type int, as well as the corresponding getBoolean () and getFloat () function.

-add_section (section) to add a new section
-set (section, option, value) of the section of the option is set
to call write the contents of the configuration file.

pip install ConfigParser

  

 https://www.cnblogs.com/wang-yc/p/5620944.html

 

 

2https://blog.csdn.net/liuchunming033/article/details/39376147

Prepare a file text.txt, content line "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD".

'''
Created on Sep 18, 2014
@author: liu.chunming
'''
#There is a text.txt file which contains "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD"
 
text_file=r"C:\Users\liu.chunming\Desktop\test.txt"
#open()
f=open(text_file,"r")
#seek() 
f.seek(8,0)
#tell
pos=f.tell()
#read()
text_to_number=f.read(8)
f.seek(8,1)
text_to_all=f.read()
 
f.close()
 
print pos
print text_to_number

  result

8
BBBBBBBB
DDDDDDDD

 

Code analysis:

The method involves several code file operations.

1.open () method

To open a file. This is the first step for file operations. Syntax open () method is as follows: open (name [, mode [, buffering]]). The name parameter is the only mandatory parameter of the open method used to identify the name of the file you want to open. file mode is the open mode, usually three: r is opened read mode, w is opened in write mode, additional mode A is open.

2.seek () method

It is provided with a current file read / write pointer offset. Syntax seek () method as follows: fileObject.seek (offset [, whence]). The offset parameter specifies the offset, where the second parameter indicates the offset reference first parameter is: 0 means move to an absolute position (counting from the beginning of the file), 1 indicates a move to a relative position (counting from the current position ), and 2 means a relative position of the end of the file. "

3.tell () method

Returns the current position of the file pointer.

4.read () method

Read the contents of the file method. Read the contents of the file are two other methods readline and readlines.

the readline () reads each line, the current position to the next line;

the readlines () to read the entire file all lines, stored in a list (list) variables, each row as an element;

Read (size) size bytes read from the current position of the file (if the file is completed, until the end of the file is read), if the size is negative or omitted, read until the end of the file, the result is a string .

5.close () method

Operating the file, be sure to close the file. Close the file is to use the close method

 

Guess you like

Origin www.cnblogs.com/kekeoutlook/p/11291408.html