修改作为Windows服务运行的python脚本的执行路径

时间:2012-11-09 19:28:05

标签: python windows-services py2exe configparser

我有一个使用ConfigParser.ConfigParser()访问配置文件的Python应用程序。我使用py2exe创建了Python应用程序的Windows服务。我遇到的问题是,如果将服务放在windows/system32文件夹中,该服务只能找到配置文件。我想将配置文件放在安装服务的同一文件夹中。例如,使用py2exe后,我有以下文件夹:

c:/temp/dist/winservice.exe
c:/temp/dist/configfile.cfg

然后我这样做:

winservice.exe install

但该服务不会在路径configfile.cfg中查找c:/temp/dist/,而是在路径中查找:c:/windows/system32/

有没有办法改变它?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用Inspect模块获取创建此代码对象的文件的名称。 因此,要获取您将使用的文件的路径:inspect.currentframe()。f_code.co_filename

因此,要获取winservice.exe所在的目录名称:

dirPath = os.path.dirname(inspect.currentframe()。f_code.co_filename)

希望有所帮助