如何编写代码以避免windows服务读取配置文件时出错?

2024-09-29 06:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有文件树:

f:/src/
   restore.ini
   config.py
   log.py
   service.py
   test.py

test.py代码如下:

^{pr2}$

现在,我使用命令行python test.py installpython test.py start来操作服务,但是出错了。在

如果我将目录src中的所有文件移动到C:\Python27\Lib\site-packages\win32\src,并更改代码:

self.currentRound = int(config.read_config_co(r'src\restore.ini', 'Record')['currentRound'])

config.write_config_update_co(self.currentRound-1, 'Record', 'currentRound', r'src\restore.ini')

现在,没事了!在

我不想移动目录src,怎么办? 谢谢!在


Tags: 文件代码pytestselfsrc目录log
1条回答
网友
1楼 · 发布于 2024-09-29 06:23:13

如果对文件名或目录名使用相对路径,python将在当前工作目录(bash中的$PWD变量;在windows上类似?)中查找(或创建它们)。在

如果您想让它们相对于当前的python文件,可以使用(python3.4)

from pathlib import Path
HERE = Path(__file__).parent.resolve()
RESTORE_INI = HERE / 'restore.ini'

或(python 2.7)

^{pr2}$

如果您的restore.ini文件与python脚本位于同一目录中。在

那你就可以用在

def setUp(self):
    self.currentRound = int(config.read_config_co(RESTORE_INI, 
                           'Record')['currentRound'])

相关问题 更多 >