Python practical small method, Python Practical Method

Source: Internet
Author: User

Python practical small method, Python Practical Method

Practical Python method used in the project

1. Get the current working directory

import os        print os.getcwd()<pre name="code" class="python">    print os.path.abspath('.')

2. format the time string 

import datetime    time_str=datetime.now().strftime('%Y%m%d-%H%M%S')

3. parse the configuration file

    config_path=r'D:\demo'    config_path=config_path.strip()    ini_file_name='Config.ini'    try:        config=ConfigParser.ConfigParser()        config.readfp(open(config_path+'\\'+ini_file_name,'rb'))                log_path=config.get('global', 'log_path')        log_name=config.get('global', 'log_name')        report_json_link=config.get('global', 'report_json_link')        proxy_address=config.get('global', 'proxy_address')        log=screenshot_log(log_path,log_name)    except Exception,e:        print 'get config info failed!!!',e        exit

4. json parsing method:

Json_str = json. dumps (a_object) # convert a Python object to a json string json_obj = json. loads (json_str) # convert a json string to a Python object


5. Determine whether a file or directory exists:

os.path.exists(your_path)

 




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.