Reputation: 9637
I have successfully created a Python Windows service using pywin32. In testing my application I attempted to have it print (which didn't work as I expected) and I also had it write to a file. It was able to write to a file, but the file ended up in the python library site-packages folder. This appears to be where the working directory is, though I'm not sure why? I would like to know the best way to specify what the working directory should be.
I could open files with full path names, or I could maybe use os.cwd? What is the best practice?
Here are the two files which compose my Windows service.
import os
import sys
import win32service
import win32serviceutil
from twisted.internet import reactor
import xpress
class XPressService(win32serviceutil.ServiceFramework):
_svc_name_ = 'XPress'
_svc_display_name_ = 'XPress Longer Name'
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
reactor.callFromThread(reactor.stop)
def SvcDoRun(self):
xpress.main()
reactor.run(installSignalHandlers=False)
if __name__ == "__main__":
win32serviceutil.HandleCommandLine(XPressService)
Below is "xpress.py" which is imported by the above script.
import datetime
def main():
with open('times', 'a') as f:
print str(datetime.datetime.now())
f.write(str(datetime.datetime.now()))
if __name__ == '__main__':
main()
Upvotes: 2
Views: 2741
Reputation: 10970
They both work, it's what your needs are. For various reasons, it's probably best to use absolute paths to the file names, this way you don't have to worry about 'where' your app is working, you just know where the output will be (which is most important). In *nix, apps generally work in '/' when they don't have a specified working directory. If you do choose to work in another directory it's os.chdir(newDir)
, do this before you call win32serviceutil.HandleCommandLine
I don't know the windows default, but you probably nailed it with the library's directory in site-packages.
Upvotes: 2