1

I want to monitor a network drive path and detect any new directory copied there or not and then automatically copy that whole directory to my local drive.

I have the following things in mind:

  1. create a python script which can maintain the no of files in the network drive path and save it to local temp file.

  2. I will run my script every 2/3 Hrs and compare the current number of directories with the last count. If the current number is more than the previous number, simply copy those directories and paste to local directory. By this simple way I can find out there are any new directories or not, correct?

But, I was wondering if there is any other better method or powerful library present in python so that I can just easily detect newly copied directory in a network path.

martineau
  • 119,623
  • 25
  • 170
  • 301
spaul2010
  • 17
  • 2
  • @LevLevitsky: I'm guessing many of those answers will not work for network mounts, so the OP may need more guidance to get an answer. – abarnert May 12 '13 at 12:15
  • Can you install software on the host that the drive is mounted from? Because generally, it's better to watch for changes on a local drive, and then send those changes elsewhere, than to try to watch for changes on a remote-mounted drive. – abarnert May 12 '13 at 12:17
  • what if one directory is added and another is removed? the count doesn't change. – muratgu May 12 '13 at 18:18

1 Answers1

0

I am using this library to monitor file system changes - https://github.com/gorakhargosh/watchdog/

Simplest example given in the link

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

Which means that you can custom your own logic to "save to local temp file" and to "compare the current number of directories with the last count".

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
  • Does this work with network shares? From a quick glance, it uses `inotify` on linux, which will not notice changes on NFS or SMB mounts. Similarly, `FSEvents` on OS X will not notice changes on NFS or SMB or (sometimes) AFP mounts. I'm not sure about ReadDirectoryChangesW on Windows, but I'm guessing it's similar. – abarnert May 12 '13 at 12:21
  • https://github.com/gorakhargosh/watchdog/#supported-platforms Windows (ReadDirectoryChangesW with I/O completion ports; ReadDirectoryChangesW worker threads) OS-independent (polling the disk for directory snapshots and comparing them periodically; slow and not recommended) – Calvin Cheng May 12 '13 at 12:22
  • So you're saying it probably works, but only by using a "slow and not recommended" method? (I'm guessing you haven't tested.) – abarnert May 12 '13 at 12:23
  • Nope. I am a Mac and Linux user. I have never tried this on Windows. :-) – Calvin Cheng May 12 '13 at 12:25
  • But have you tested on Mac or Linux with a remote share? I don't know whether it will work on Windows, but I'm 99% sure that it _won't_ on Mac or Linux (except possibly by the "slow and not recommended" method). – abarnert May 12 '13 at 12:35