【问题标题】:Exlude a file from a directory listing从目录列表中排除文件
【发布时间】:2014-06-11 13:23:31
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    以下代码必须替换。

    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"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 2023-04-10
      • 2014-06-16
      • 2011-08-01
      相关资源
      最近更新 更多