The objective is to watch a folder for change. And if there is new subfolder created inside that watch-folder then - get the new subfolders path and get contents in it.
I have heard about libraries like watchdog and firstnotication. But I am not able to write code in python for Windows Os. Any Help is appreciated.
Pseudo-code is like this,
Watchfolder="C:/watchfolder"
if newFolderCreated inside WatchFolder:
print snewsubfolder created name #eg:- C:/watchfolder/newfolder
cd into newsubfolder
get .mp4 filepath #eg:- C:/watchfolder/newfolder/hello.mp4
答案 0 :(得分:0)
下面的代码是使用win32.file包装器API和Windows OS中可用的FindFirstChangeNotification API完成的。 以下代码的所有文档可在以下位置找到: http://timgolden.me.uk/pywin32-docs/win32file.html 您可以有更简单的方法来执行此操作,但这是最有效的方法。 所有信息都可以在 http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
import os
import win32file
import win32con
ACTIONS = {
1: "Created",
2: "Deleted",
3: "Updated",
4: "Renamed from something",
5: "Renamed to something"
}
FILE_LIST_DIRECTORY = 0x0001
path_to_watch = "C:/yourpath"
hDir = win32file.CreateFile(
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
while 1:
#Right now only directory check is being used and everything else like filecheck is commented
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:
full_filename = os.path.join(path_to_watch, file)
print(full_filename,ACTIONS.get(action, "Unknown"))
您将在full_filename中获得完整路径,并在Actions.get(操作,“未知”)中获得操作。现在监视它的更新,删除和创建。您可以使用if在代码顶部或底部的操作中进行排序。 之后,您可以使用glob库获取并打印.mp4路径
import glob
print(glob.glob(full_filename,"/*.mp4"))
这将返回目录中所有.mp4文件路径的数组