场景:手动执行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

  

相关文章:

  • 2022-12-23
  • 2021-12-07
  • 2021-06-03
  • 2021-04-07
  • 2021-09-18
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2021-06-12
  • 2022-12-23
相关资源
相似解决方案