#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : file_tools.py
@Time : 2020/10/27 13:35:58
@Author : manman
@Contact : 408903228@qq.com
@Department : my-self
@Desc : None
'''
# here put the import lib
import subprocess
from os import sep
from os import path,listdir,getcwd
import sys
sys.setrecursionlimit(2000)
# TODO 系统主目录
home = path.expanduser("~")
from sys import exc_info,stdout
import traceback
import time
def shorterror(func):
def In(*vars):
try :
return func (*vars),
except Exception as e :
exc_type, exc_value, exc_traceback_obj = exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback_obj, limit=2, file=stdout)
print("exc_type: %s" % exc_type)
print("exc_value: %s" % exc_value)
print("exc_traceback_obj: %s" % exc_traceback_obj)
return In
# TODO 文件操作函数集
def join_dir(*dir_):
'''
路径拼接函数
'''
join_dir = "".join(["{}{}".format(d,sep) for d in dir_])
return path.join(home,join_dir)#"{0}{1}{2}".format(home,sep,join_dir)
import os
@shorterror
def listdir_function(dirpath):
'''
列出文件夹下所有文件夹
'''
dirpath_list = {dirpath:[]}
if os.path.isdir(dirpath):
for name in os.listdir(dirpath):
if os.path.isdir(name):
dirpath_ = os.path.join(dirpath,name)
dirpath_list[dirpath].append(dirpath_)
return dirpath_list
else:
return False
@shorterror
def listfile_function(dirpath,select_file_type=False):
'''
列出文件夹下所有文件,默认不设置条件
'''
dirpath_list = {dirpath:[]}
if os.path.isdir(dirpath):
for name in os.listdir(dirpath):
if os.path.isfile(name):
dirpath_ = os.path.join(dirpath,name)
if select_file_type != False:
if dirpath_.split('.')[-1] in select_file_type:
dirpath_list[dirpath].append(dirpath_)
else:
dirpath_list[dirpath].append(dirpath_)
return dirpath_list
else:
return False
@shorterror
def deeping_search_function(root,target,add_list=[]):
'''
递归深度搜索
'''
items = os.listdir(root)
for item in items:
path = os.path.join(root, item)
add_list.append(path)
if os.path.isdir(path):
make = {'[-]': path}
deeping_search_function(path, target,add_list)
elif target in path.split(os.sep)[-1]:
make = {'[+]': path}
else:
make = {'[!]': path}
if list(make)[0]=='[+]':
print(make)
print(make)
return [path for path in add_list if target in path]
# TODO 分级文件夹目录
# 项目目录
project_name = "linux_tools_for_chinese"
project_dir = join_dir(project_name)
# 源码目录
srcdir = join_dir(project_name,"src")
# bash脚本文件夹
bash_functional_script_dir = join_dir(project_name,"bash_functional_script")
# python脚本文件夹
python_functional_script_dir = join_dir(project_name,"python_functional_script")
# 机器学习算法模块目录
ml_model_dir=join_dir(project_name,'3_ml_model')
# 搜索引擎文件夹
search_moduel_dir=join_dir(project_name,'5_search_moduel')
# 数据预处理模块文件夹
data_preprocessing_dir=join_dir(project_name,'0_data_preprocessing')
# python脚本文件夹
python_functional_script_dir=join_dir(project_name,'7_python_functional_script')
# bash脚本文件夹
bash_functional_script_dir=join_dir(project_name,'6_bash_functional_script')
# 数据结构模块文件夹
data_structure_dir=join_dir(project_name,'1_data_structure')
# 问答数据文件夹
question_data_dir=join_dir(project_name,'4_question_data')
# 基础数学计算模块文件夹
math_module_dir = join_dir(project_name,'2_math_module')
# 静态缓存文件夹
static_cache_dir = join_dir(project_name,"8_static_cache")
# 配置文件目录
info_table = "{}{}".format(question_data_dir,"question_file.csv")
# 每篇文章onehot矩阵列表
onehot_matrix_static_list = "{}{}".format(static_cache_dir,"onehot_matrix_list")
# 分段词频表地址
word_frequency_of_each_articles_static_table = "{}{}".format(static_cache_dir,"word_frequency_of_each_article.csv")
# 分段特征概率表地址
eigenvector_static_table = "{}{}".format(static_cache_dir,"eigenvector_table.csv")
# 特征字典地址
feature_static_dictionary = "{}{}".format(static_cache_dir,"static")
# 特征向量地址
feature_static_vector = "{}{}".format(static_cache_dir,"feature_vector")
# 当前单词计数
if __name__ == "__main__":
print(ml_model_dir
, search_moduel_dir
, data_preprocessing_dir
, python_functional_script_dir
, bash_functional_script_dir
, data_structure_dir
, question_data_dir
, math_module_dir)
#print(listdir_function(os.getcwd()))
#print(listfile_function(os.getcwd(),select_file_type=['py']))
#print(listdir(home))
#{"root":,"target":}
print(deeping_search_function(project_dir,'push'))
10-25
09-05