3

特定の種類のファイルのフォルダーを監視するスクリプトをセットアップする必要があります。私はこのコードを作成しましたが、より良い方法があるかどうか疑問に思っていましたか?

import os


def listAppleseedFiles(directory_path):
    directory_entities =  os.listdir(directory_path)
    files = []
    appleseed_files = []
    for entity in directory_entities:
        file_path = os.path.join(directory_path, entity)
        if os.path.isfile(file_path):
            if os.path.splitext(file_path)[1] == '.appleseed':
                appleseed_files.append(file_path)

    return appleseed_files

while True:
    for file in listAppleseedFiles('/dir_name'):

        doSomething()
4

2 に答える 2

5

ウォッチドッグを試してみてください!彼らの例から:

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

event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path='/dir_name', recursive=True)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()
于 2012-08-09T11:33:31.990 に答える
0

私はwatcherPyPi から正常に使用しました。これは API のラッパーReadDirectoryChangesWです。以下に例を示します (Py3 ですが、Py2 で実行されます)。

from watcher import *
import time

def callback(*stuff):
    if stuff[0] == FILE_ACTION_REMOVED:
        print(stuff[1],"deleted")
    else:
        print(stuff)

w = Watcher('C:\\' , callback)
w.flags = FILE_NOTIFY_CHANGE_FILE_NAME
w.start()

while True:  
    time.sleep(100)
于 2012-08-09T12:18:34.363 に答える