0
点赞
收藏
分享

微信扫一扫

php 批量添加namespace

一葉_code 2022-01-16 阅读 65
import os


def deal(parent_path, filename_full_path, namespace_prefix):
    filename = os.path.split(filename_full_path)[-1]
    # com\alibaba\china\openapi\client\example
    new_path = filename_full_path.replace(parent_path, "").replace(filename, "")[1:-1]

    namespace = "namespace " + namespace_prefix + new_path + ";"
    with open(filename_full_path, encoding='utf-8', mode='r+') as f:
        content = f.read().replace("<?php\n", "")
        f.seek(0, 0)
        f.write("<?php\n" + namespace + "\n" + content)
        f.close()

    pass


def main():
    parent_path = "K:\develop\project\php\demo\youmeng";
    namespace_prefix = "App\\Umeng\\"
    # 批量添加namespace|
    # 列出windows目录下的所有文件和文件名
    for (root, dirs, files) in os.walk(parent_path):

        for filename in files:
            filename_full_path = os.path.join(root, filename)
            if filename_full_path.__contains__(".idea"):
                continue
            if not filename_full_path.endswith(".php"):
                continue
            if filename_full_path.endswith("Example.php") or filename_full_path.endswith("HttpClient.class.php"):
                continue
            deal(parent_path, filename_full_path, namespace_prefix)
    print("添加完成")
    pass


if __name__ == '__main__':
    main()

新加的

 

举报

相关推荐

0 条评论