从目录列表中排除文件

时间:2014-06-11 13:23:32

标签: file directory python-3.4 windows-xp-sp3

我测试了这个program,它检查目录是否删除,重命名或修改了文件。我想知道如何通过排除某些文件检查一个目录?我该如何修改变量 PATH_TO_WATCH = ["C:/myDirectory"]以便程序探测C:/myDirectory但从检查中排除2个文件(file1.txt和file2.txt)?

1 个答案:

答案 0 :(得分:0)

必须替换以下代码。

for action, file in results:
  full_filename = os.path.join (path_to_watch, file)
  if not os.path.exists (full_filename):
    file_type = "<deleted>"
  elif os.path.isdir (full_filename):
    file_type = 'folder'
  else:
    file_type = 'file'
  yield (file_type, full_filename, ACTIONS.get (action, "Unknown"))

通过此代码:

for action, file in results:
  if (file != 'file1.txt' and file != 'file2.txt'): #here you cut the check of the files you don't want.
    full_filename = os.path.join (path_to_watch, file)
    if not os.path.exists (full_filename):
      file_type = "<deleted>"
    elif os.path.isdir (full_filename):
      file_type = 'folder'
    else:
      file_type = 'file'
    yield (file_type, full_filename, ACTIONS.get (action, "Unknown"))