Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:41:51+00:00 2026-05-13T12:41:51+00:00

I am fairly new to python, and have no experience with writing services for

  • 0

I am fairly new to python, and have no experience with writing services for windows. I have tried to hack together a windows service based on afew tutorials i have found out there.

I need this service to constantly monitor a directory for changes and when it sees a change it runs a script. Here is what i have so far:

import win32service
import win32serviceutil
import time

class aservice(win32serviceutil.ServiceFramework):
    _svc_name_ = "aservice"
    _svc_display_name_ = "aservice - It Does nothing"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.isAlive = True

    def SvcDoRun(self):
        import servicemanager
        while self.isAlive:
            # This is where i am trying to run my code...is that right?
            self.main()
            #servicemanager.LogInfoMsg("aservice - is alive and well")
            #time.sleep(5)
            #servicemanager.LogInfoMsg("aservice - Stopped")

    def SvcStop(self):
        import servicemanager
        servicemanager.LogInfoMsg("aservice - Recieved stop signal")
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.isAlive = False #this will make SvcDoRun() break the while loop at the next iteration.

    # This code monitors a directory for changes and calls a script when there is a change
    # At the moment, it creates a log file instead of calling the script
    def main(self):
        import os
        import win32file
        import win32con
        ACTIONS = {
            1: "Created",
            2: "Deleted",
            3: "Updated",
            4: "Renamed from something",
            5: "Ranamed to something"
        }

        FILE_LIST_DIRECTORY = 0x0001

        path_to_watch = "folder"
        hDir = win32file.CreateFile (
            path_to_watch,
            FILE_LIST_DIRECTORY,
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
        )
        while 1:
            results = win32file.ReadDirectoryChangesW (
                hDir,
                1024,
                True,
                win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
                win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
                win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
                win32con.FILE_NOTIFY_CHANGE_SIZE |
                win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
                win32con.FILE_NOTIFY_CHANGE_SECURITY,
                None,
                None
            )
            for action, file in results:
                #import script
                log = open ('log.txt', 'w')
                full_filename = os.path.join(path_to_watch)
                log.write(full_filename + ACTIONS.get(action, "Unknown"))
                log.close()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(aservice)

Any ideas on what i am doing wrong?

EDIT: Here is the event log error i am getting for it.

Event Type: Error
Event Source:   aservice
Event Category: None
Event ID:   3
Date:       21/01/2010
Time:       11:27:43 AM
User:       N/A
Computer:
Description:
The instance's SvcRun() method failed 
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\win32\lib\win32serviceutil.py", line 806, in SvcRun
    self.SvcDoRun()
  File "D:\TEST\simpleservice2.py", line 35, in SvcDoRun
    self.main()
  File "D:\TEST\simpleservice2.py", line 72, in main
    None
error: (2, 'CreateFile', 'The system cannot find the file specified.') 
%2: %3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:41:51+00:00Added an answer on May 13, 2026 at 12:41 pm

    I’m not familiar with pywin32’s ServiceFramework, but based on your code and the error message, I bet you have a problem with path_to_watch.

    I assume you sanitized the folder name for your example, and you’re not actually looking for folder.

    How about:

    • Are you using the full path to the folder? The current working directory of the service may not be what you expect.
    • Are you escaping any backslashes in the folder path?

    Try using:

    path_to_watch = r"c:\foo\bar" + "\\" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.