批量删除编译器产生目标文件

编译较大程序库的时候会产生大量目标文件,如Qt,有一半的空间被目标文件占据,然而编译完后,这些文件往往就没什么用了。

下面的python程序就是用来批量删除目标文件的,使用的时候将root_path修改为需要处理的路径,另外默认删除.obj,.o,.pch文件。

import os
import shutil

FILE_COUNT = 0

def list_obj_file(f, path):
    (name, ext) = os.path.splitext(f)
    if ext == ".obj" or ext == ".o" or ext == ".pch":
        global FILE_COUNT
        FILE_COUNT = FILE_COUNT + 1
        full_file_name = os.path.join(path, f)
        print("delete: ", full_file_name)
        os.remove(full_file_name)
        #new_file_name = full_file_name + ".obj"
        #shutil.move(full_file_name,new_file_name)

def traverse_dir(dir):
    for f in os.listdir(dir):     
        if os.path.isdir(os.path.join(dir, f)):
            print("dir", os.path.join(root_path, f))
            traverse_dir(os.path.join(dir, f))
        else:
            list_obj_file(f, dir)
             
root_path = "K:/qt-everywhere-opensource-src-4.7.1"

for f in os.listdir(root_path):
    if os.path.isdir(os.path.join(root_path, f)):
        print("dir:", os.path.join(root_path, f))
        traverse_dir(os.path.join(root_path, f))
    else:
        list_obj_file(f, root_path)
       
print("DELETE OBJ FILES COUNT: ", FILE_COUNT)
 

你可能感兴趣的:(批量删除编译器产生目标文件)