Why running a Python script as a service in Windows can not write data to text?
<p>I want to reproduce the same result as <a href="http://www.chrisumbel.com/article/windows_services_in_python" rel="nofollow">Windows Services in Python</a>:</p>

<pre><code>import win32service  
import win32serviceutil  
import win32event  

class PySvc(win32serviceutil.ServiceFramework):  
    # you can NET START/STOP the service by the following name  
    _svc_name_ = "PySvc"  
    # this text shows up as the service name in the Service  
    # Control Manager (SCM)  
    _svc_display_name_ = "Python Test Service"  
    # this text shows up as the description in the SCM  
    _svc_description_ = "This service writes stuff to a file"  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

    # core logic of the service     
    def SvcDoRun(self):  
        import servicemanager  
        print 'hello'

        f = open('test.dat', 'w+')  
        rc = None  

        # if the stop event hasn't been fired keep looping  
        while rc != win32event.WAIT_OBJECT_0:  
            f.write('TEST DATA\n')  
            f.flush()  
            # block for 5 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)  

        f.write('SHUTTING DOWN\n')  
        f.close()  

    # called when we're being shut down      
    def SvcStop(self):  
        # tell the SCM we're shutting down  
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
        # fire the stop event  
        win32event.SetEvent(self.hWaitStop)  

    if __name__ == '__main__':  
        win32serviceutil.HandleCommandLine(PySvc)  
</code></pre>

<p>In the <code>cmd</code> administrator mode,I type these commands,it works good.And telling me <code>Python Test Service</code> start.</p>

<blockquote>
  <p>python.exe .\PySvc.py install</p>
  
  <p>NET START PySvc</p>
</blockquote>

<p>But the file <code>test.dat</code> isn't been created.So how to make the program into <code>SvcDoRun</code> function? </p>

<p>Converting Comment to Answer...</p>

<p>Try using a full path for the test.dat file (C:\servicedata\test,dat). The path when you run in the interperter is different from when you run as a service.</p>

python windows