好的,所以我编写了一个只写入文件的python服务。我用cx_freeze打包它,并通过命令提示符将其安装为服务。它运行正常,但后来我意识到它要编写的文件位于一些奇怪的目录中,所以我更改了服务代码以将文件写入我的文档。这是我的Python服务的代码:
import win32service
import win32serviceutil
import win32event
class PySvc(win32serviceutil.ServiceFramework):
_svc_name_ = "PySvc"
_svc_display_name_ = "Python Test Service"
_svc_description_ = "This is a service that simply writes to a file"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcDoRun(self):
import servicemanager
f = open('C:/Users/Luke/Documents/test.dat', 'w+')
rc = None
while rc != win32event.WAIT_OBJECT_0:
f.write('TEST DATA\n')
f.flush()
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
f.write('SHUTTING DOWN...\n')
f.close()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
if __name__ == '__main':
win32serviceutil.HandleCommandLine(PySvc)
这一行:
f = open('C:/Users/Luke/Documents/test.dat', 'w+')
用于看起来像这样(当它工作时):
f = open('test.dat', 'w+')
这是我对代码所做的唯一更改。 这次当我试图在cmd中安装它时,它返回了这个令人沮丧的错误:
Exception occurred while initializing the installation:System.BadImageFormatException: Could not load file or assembly 'file:///C:\PySvc\PySvc.exe' or one of its dependencies. The module was expected to contain an assembly manifest..
发生了什么?任何帮助?
答案 0 :(得分:0)
我找到了解决方案,并意识到我真正的业余爱好者。在某个地方,我意外地删除了if __name__ == '__main__':
中的最后2'_,如您在代码中所见。只是告诉你应该在提出问题之前检查所有代码!所以我改变了这个:
if __name__ == '__main':
对此:
if __name__ == '__main__':