简体   繁体   English

创建监视目录的文件列表

[英]Creating a list of files of watched directory

I set up a watchfolder in one directory and want it to append each filmename into a list everytime a new file shows up.我在一个目录中设置了一个监视文件夹,并希望它在每次出现新文件时将每个电影名称放入列表中。

Here's what im trying to do:这就是我想要做的事情:

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')
    
    
#set file path

    #path = sys.argv[1] if len(sys.argv) > 1 else '.'
    path = "C:\\Users\\Ingest\\Downloads"

#logging
    event_handler = LoggingEventHandler()
    
#start observing
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    
#start
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()



var_1 = observer
directory_evaluation = []

for file in var_1:

    if file == "%.mxf" or "%.mov":
        directory_evaluation.append(file)
        print(directory_evaluation)




Apparently i cannot associate "observer" to a variable because it's not a iterable.显然我不能将“观察者”与变量相关联,因为它不是可迭代的。 What can I do to fix it?我能做些什么来修复它?

Based on your comments, I assume that you currently have two lists of filenames that are located in two directories, files_dir_A and files_dir_B .根据您的评论,我假设您当前有两个文件名列表,它们位于两个目录files_dir_Afiles_dir_B中。 You want the intersection of these lists.你想要这些列表的交集。 As filenames in a directory have to be unique, you can convert the lists to sets and use the built-in intersection operator for sets:由于目录中的文件名必须是唯一的,您可以将列表转换为集合并使用内置的交集运算符进行集合:

files_dir_A = [f for f in os.listdir('path/to/dir/A') if f.endswith(extension)]

files_dir_B = [f for f in os.listdir('path/to/dir/B') if f.endswith(extension)]

result = list(set(files_dir_A) & set(files_dir_B))

So given a concrete example, the lists look like this:因此,给出一个具体示例,列表如下所示:

files_dir_A = ['file1', 'file2', 'file3']
files_dir_B = ['file2', 'file3', 'file4']

result = ['file2', 'file3']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM