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)