"Python" Profile relative path & working directory for automatic software execution

Source: Internet
Author: User

A few changes were made to the monitoring scripts today, and then all of a sudden, the monitoring was completely invalidated. A half-day problem is still unknown. Finally found it was a step on several times the old pit.

is the script written in the configuration file for debugging convenience written relative path, but on-line not aware of the software automatic execution (such as Zabbix ExternalCheck, although all scripts can be placed in a unified directory, Giving a kind of this directory is the illusion of the working directory when Zabbix performs an external check, but it's not actually. ), the working directory that is automatically executed is not necessarily the current directory. In this pit has been stepped several times, the following examples and solutions:

 import   CONFIGPARSERCF  = Configparser () conf_file  =  "   " #   When writing scripts locally for debugging convenience, create a configuration file in the same directory as the script to configure  cf.read (conf_file)  #   face conf_file do some processing  #   The same script, let Zabbix to run their own error??? Then go to the directory to do their own manual execution, no problem??? is the path of the pot!  #   change path to absolute path ok  conf_file =  '  

Think about it, this pot is actually configparser this module itself a small "flaw" bar. because Configparser.configparser (). Read (path), there is no error even if path does not exist. If there is an error, we can locate the problem quickly. So remember to remember, before using Configparser must os.path.isfile (path) Check!

  

If you want to preserve the flexibility of configuration files and scripts on porting, you can do this:

Work_dir = Os.path.dirname (Os.path.abspath (__file__= Os.path.join (Work_dir,'  server.conf')# so that you can guarantee the absolute path of conf_file, and will not write dead. 

Similar operations have been written many times and are no longer repeated.

"Python" Profile relative path & working directory for automatic software execution

Related Article

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.