比较文件夹大小和 运行 脚本(如果大小发生变化)

Compare folder size and run script if the size changed

我正在尝试编写一个代码来对文件夹中新添加的文件执行一些操作。所以,我的看法是计算文件夹大小,与±10分钟前计算的比较,如果文件夹大小发生变化,则对新添加的文件进行处理。

while (True):
    def get_size(start_path='/Users/.../Desktop/files'):
        total_size = 0
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                # skip if it is symbolic link
                if not os.path.islink(fp):
                    total_size += os.path.getsize(fp)

        return total_size

    print(get_size(), 'bytes')
    time.sleep(10)

上面的代码每 10 秒计算一次文件夹的大小。我不知道如何将它与以前的尺寸进行比较:(

求求你帮忙..

这是一个简单的问题,可以用这样的最小可重现示例来代替:

while True:
    a = input() # How can I check if this is different from the previous input?

创建一个存储先前权重的变量,这样:

old = get_size()
while True:
    new = get_size()
    if new != old:
        # Something changed
    old = new # You can do it since integers are immutable typed

我给你加个建议:

def get_size():
    ...
while True:
    ...

这将使您避免浪费时间和效率,否则每次 while 循环执行时(每 10 秒!)都会重新定义 get_size 函数。

您可以尝试类似的方法:

before = 0
while (True):
    def get_size(start_path='.'):
        total_size = 0
        # use variable defined outside loop
        global before
        for dirpath, dirnames, filenames in os.walk(start_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                # skip if it is symbolic link
                if not os.path.islink(fp):
                    total_size += os.path.getsize(fp)

        # Your logic
        if total_size > before:
            print("New: "+str(total_size - before))
        else:
            print("No changes")
        before = total_size
        return total_size

    print(get_size(), 'bytes')
    time.sleep(10)

跟踪目录的总大小是有限的。您如何保留文件及其大小的列表?这样您就可以对更改的文件和新文件进行操作。在这里使用字典作为基本示例,您真的可以根据需要将其复杂化,跟踪创建、修改日期等。如果您不想要复杂性,我保留了对总大小的跟踪,但是您仍然需要跟踪哪个文件已更改。

import os
import time

def check_dir(fh,start_path='/tmp',new_cb=None,changed_cb=None):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                fs = os.path.getsize(fp)
                total_size += fs
                if f in fh:
                    if fh[f] == fs:
                        # file unchanged
                        pass
                    else:
                        if changed_cb:
                            changed_cb(fp)
                else:
                    #new file
                    if new_cb:
                        new_cb(fp)
                fh[f] = fs

    return total_size

def new_file(fp):
    print("New File {0}!".format(fp))

def changed_file(fp):
    print("File {0} changed!".format(fp))

if __name__ == '__main__':
    file_history={}
    total = 0

    while(True):
        nt = check_dir(file_history,'/tmp/test',new_file,changed_file)
        if total and nt != total:
            print("Total size changed from {0} to {1}".format(total,nt))
            total = nt
        time.sleep(10)
        print("File list:\n{0}".format(file_history))