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

  

最新文章

  1. android studio每次启动都要在fetching Android sdk compoment information停好久的解决方案
  2. canvas ---个性时钟
  3. Android数据缓存(转)
  4. 查询sqlserver 正在执行的sql语句的详细信息
  5. 免费的API接口
  6. C# 多线程参数的使用
  7. OK335xS 网络连接打印信息 hacking
  8. 使用JS制作一个鼠标可拖的DIV(三)——移动带图片DIV
  9. Fedora 21 设置开机启动脚本
  10. Java实现字符串反转
  11. QT5控件-QPushButton和QFocusFrame(按钮和焦点框)
  12. sql执行顺序整理
  13. file_get_contents 超时设置
  14. Nodejs的运行原理-模块篇
  15. 进阶路上有你我-相互相持篇之ES6里箭头函数里的this指向问题
  16. springmvc sessionfilter 登录过滤器
  17. MySQL数据库之part2
  18. 第二章 向量(d5)有序向量:插值查找
  19. Merge into 详细介绍
  20. PyCharm/IDEA 使用技巧总结

热门文章

  1. Qt中使用DOM解析XML文件或者字符串(实例)
  2. TRIZ系列-创新原理-9~11-预先反作用原理、预处理原理、预先防范原理
  3. MySQL binlog日志三种模式选择及配置
  4. C/C++中获取数组的长度
  5. py自动化之环境配置
  6. org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xm
  7. Spring Boot干货系列:(一)优雅的入门篇
  8. registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
  9. JavaScript高级 面向对象(5)--内存逻辑图画法
  10. html5标签video(播放器)学习笔记(二)-基本操作