ConfigParser.NoSectionError: No section: 'MongoDB'


场景:手动执行bat文件正常,schtasks定时执行bat文件时报错。

原因:定时执行时,ini配置文件找不到。Windows 下用 schtasks 定时执行脚本的默认起始路径为:C:\Windows\System32

解决方法:

import os

  self._config = ConfigParser.SafeConfigParser()
      batRunningPath = os.path.dirname(os.sys.executable) #获取可执行的bat文件所在路径,缺点:手动执行时,获取的是python的安装路径,而不是可执行bat文件所在的路径。
      batPath = os.path.dirname(os.sys.path[0])      #获取可执行的bat文件所在路径的上一级路径,缺点:文件所在文件夹名字写死了(dbMonitor)不能更改。
      if os.path.exists(batPath + "\dbMonitor\config.ini"):  #做个判断,避免上述缺点
          self._config.read(batPath + "\dbMonitor\config.ini")
      else:
          self._config.read(batRunningPath + "\config.ini")

  #end

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM