前言
我们常用Python代码做一些文件操作、数据分析、自动化工具等。
通用的文件夹/文件处理框架
# encoding = utf-8
import os
import sys
import subprocess
import time
class operate_folder(object):
def __init__(self):
# self.arg = arg
self.successful_um=0
self.failed_num=0
def init_args(self,input_folder,output_folder,other_params):
self.input_folder=input_folder
self.output_folder=output_folder
self.exList=[] #多线程
self.exe_params=other_params #可以是一个turple
self.max_process=12
def set_process_max(self,num):
self.max_process=num
def create_dir(self,dir_path):
if not os.path.exists(dir_path):
os.makedirs(dir_path)
def do_some_action(self,input_folder,output_folder):
try:
folder_or_file_list = os.listdir(input_folder)
ex=subprocess.Popen(self.exe_params[1]+" "+input_folder+" "+output_folder)
self.exList.append(ex)
curList=self.exList
for item in curList:
if item.returncode == 0:
self.exList.remove(item)
if(len(self.exList)>self.max_process):
exList[0].wait()
self.successful_num=self.successful_num+1
for item in folder_or_file_list:
abs_path=input_folder + "\\" + item
if os.path.isdir(abs_path):
do_some_about_folder(abs_path,output_folder)
do_some_action(abs_path,output_dir+"\\"+item)
elif os.path.isfile(abs_path):
do_some_about_file(abs_path,output_folder)
except Exception as e:
self.failed_num=self.failed_num+1
def do_some_about_folder(self,cur_folder,output_folder):
pass
def do_some_about_file(self,cur_file,output_folder):
pass
if __name__ == '__main__':
input_dir=""
output_dir=""
exe_process_file=""
if len(sys.argv)>2:
input_dir=sys.argv[1]
output_dir=sys.argv[2]
exe_process_file=sys.argv[3]
if not os.path.isdir(input_dir) or not os.path.isdir(output_dir) or input_dir=="" or output_dir=="":
print("输入或输出目录不正确")
else:
operate=operate_folder()
operate.init_args(input_dir,output_dir,(exe_process_file,))