基本OS库的使用

  1. 获取目录
  2. 获取文件
  3. 获取哦thinkphp所有action

获取目录

os.chdir(filepath)
items = os.listdir(filepath)
for name in items :
    # 遍历所有文件
    if not os.path.isdir(name):
        if keyword in name :
            new_name = name.replace(keyword,'')
            os.renames(name,new_name)
    else:
        rename(filepath + '\\' + name, keyword)
        os.chdir('...')      
items = os.listdir(filepath)

os.listdir

获取文件

获取哦thinkphp所有action

import os,re
dirpath = "D:\\www\\meeshine\\oatest\\Lib\\Action\\"
os.chdir(dirpath)

items = os.listdir(dirpath)

group_name = {}
class_name = {}
action_list = {}

dirlist = []
r'public function +[_0-9a-zA-Z]+('
items = os.listdir(dirpath)
for name in items:
    if os.path.isdir(name):
        dirlist.append(name)
        # print("是文件夹:",name)
        # group_name.append(name)
    else:
        # name = name.replace("Action.class.php",'')
        if 'public' not in  group_name.keys():
            group_name['public'] = [name]
        else:
            value_list = group_name['public']
            value_list.append(name)
            group_name['public'] = value_list


for x in dirlist:
    if os.path.isdir(name): continue;


    os.chdir(dirpath+x)
    items = os.listdir(dirpath+x+"\\")
    for name in items:
        # name = name.replace("Action.class.php",'')
        if x not in  group_name.keys():
            group_name[x] = [name]
        else:
            value_list = group_name[x]
            value_list.append(name)
            group_name[x] = value_list

action_list = {};



gn_list = []

test_list = []

for x in group_name:
    # with open(dirpath+'\..\\test.json', 'a+') as f:
    # gn_list[x] = [];
    if x in gn_list:
        continue;

    gn_list.append(x)

    cn_list = []

    # print(gn_list)
    for i in group_name[x]:
        openfile =''
        if x !='public' :
            openfile = dirpath+"\\"+x+"\\"+i
        else:
            openfile = dirpath+"\\"+i

        # 组名称
        gn = x
        # 控制器名称
        cn = i.replace("Action.class.php",'')
        if cn in cn_list:
            continue;
        cn_list.append(cn)

        with open(openfile,'r+',encoding='utf-8') as s:
            an_list = []
            while True:
                line = s.readline()
                if not line:
                    break
                m = (re.search(r'public\s*function(.*?)\(',line))
                if(m):
                    if(line.find("//") != -1):break;
                    an = line.replace("public function",'').replace("(){",'').replace("() {",'').strip()
                    if an in an_list:
                        continue;
                    an_list.append(an)
                    test_list.append([gn,cn,an])


import pandas as pd
df=pd.DataFrame(test_list)
data = df.to_csv('Result1.csv',index=None)
print("ok")

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 anaf@163.com
目录