Common modules of Python Basics (iii)

Source: Internet
Author: User

1.configparser Module

This module is used to read and write files, for files that are similar in format to Windows INI files, and can contain one or more sections (section), each of which can have multiple parameters (key-value pairs)

The format of the configuration file is as follows:

= = =123= = Fuck

This kind of file format is like a big dictionary, each title is a key, the dictionary is nested in the dictionary

It is also important to note that the key-value pairs in [default] are common, and [default] can not write

How to write such a file in Python?

ImportCONFIGPARSERCFP=configparser. Configparser ()#It 's an empty dictionary .cfp['DEFAULT']={"Serveralagas": 24,"ASDGASG":'Yes',"SDG": 123}cfp['Hello']={'User':'HD'}cfp[' World']={' What':'Fuck'}with Open ('Cfp.ini','W') as F:cfp.write (f)

Read File contents

#Read FileImportConfigparserconfig=Configparser. Configparser () Config.read ('Cfp.ini')#View all titlesres=config.sections ()Print(RES)#[' Hello ', ' world ']#See key for all key-value pairs under Title HelloOptions=config.options ('Hello')Print(options)#[' User ', ' serveralagas ', ' asdgasg ', ' SDG ']#View the (key,value) format of all key-value pairs under header HelloItem_list=config.items ('Hello')Print(item_list)#[(' Serveralagas ', ' "'), (' Asdgasg ', ' yes '), (' SDG ', ' 123 '), (' User ', ' HD ')]#View the value of user under the Hello heading as a stringVal=config.get ('Hello','User')Print(Val)#HD#the above command, get can be changed to Getint, view the integer format, change to Getboolean, view the boolean value format#change to GetFloat view floating-point format

Modify File Contents

ImportConfigparserconfig=Configparser. Configparser () Config.read ('Cfp.ini')#Delete entire caption HelloConfig.remove_section ('Hello')#Delete Title What's under worldConfig.remove_option (' World',' What')#is there a title in the sentence?Print(Config.has_section ('Hello'))#determine if there is a user in the title WorldPrint(Config.has_option (' World','User'))#Add a titleConfig.add_section ('Zhang') #如果已经存在则会报错#Add a name=zhang,age=18 configuration under the headingConfig.set ('Zhang','name','Zhang') Config.set ('Zhang',' Age', 18)#will error, typeerror:option values must be strings                            #must be a string form#write the modified content to the file, complete the final modificationConfig.write (Open ('Cfp.ini','W'))

2.subprocess Module

This module allows a process to create a new child process, connect to the stdin/stdout/stderr of the child process through a pipeline, and get the return value of the child process.

This module has only one Popen class

Import subprocess # Create a new process that is out of sync with the main process s=subprocess. Popen (' dir ', shell=true)#S is an instantiated object of Popen s.wait ()  #手动控制子进程的执行稍后一点 print (' Ending ') #主进程的命令
# in the parent process, of course, you can have more action on the child process S.poll ()    # View the status of the child process S.kill ()    #  Terminates a subprocess s.send_signal ()# sends a signal to a child process # terminating a child process

You can also change standard input, standard output, and standard errors when Popen () is established and use subprocess. Pipe connects the input and output of multiple child processes together to form a pipeline

Import subprocesss1=subprocess. Popen (['ls','-l'],stdout=subprocess. PIPE)print(S1.stdout.read ())

Common modules of Python Basics (iii)

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.