Python3实现删除本地目录中文件大小为0的文件

from os.path import isdir, abspath, getsize, join
from os import listdir, system
filenames = []


def append_filename(path):
    contents = listdir(abspath(path))
    for content in contents:
        content = join(path, content)
        if isdir(content):
            append_filename(abspath(content))
        else:
            filenames.append(abspath(content))
    return filenames


def main():
    path = input("[*] Please input the directory path:")
    append_filename(path)
    print(filenames)
    for filename in filenames:
        if getsize(filename) == 0:
            system('del %s' % filename)
            print("[-] Deleting %s ..." % filename)


if __name__ == '__main__':
    main()

代码乱写,没优化

你可能感兴趣的:(python)