python项目多级目录导入失效

本文主要介绍了如何在Python脚本中处理多层级文件夹结构,特别是解决不同文件夹下的子文件夹间相互调用时可能出现的路径错误,并通过Pathlib和shutil库实现路径管理和文件复制操作。
摘要由CSDN通过智能技术生成

主要是为了解决python脚本运行时,多个文件夹下的子文件夹相互调用时报错的问题。

from pathlib import Path
import shutil

destination_path = "/www/wwwroot/pythonEnv/pyETL/lib/python3.9/site-packages/ali_bigdata_myapplications.pth"
local_path = "/www/wwwroot/PythonProject/MyUtils/AddPath2SitPackages/ali_bigdata_myapplications.pth"
local_path = Path(local_path)

if local_path.exists():
    Path(local_path).unlink()


def get_all_dir(pth, dir_list):
    path = Path(pth)
    for path_single in path.iterdir():
        # 递归查询指定路径下所有目录
        if path_single.is_dir():
            dir_list.append(str(Path(path_single).absolute()))
            get_all_dir(path_single, dir_list)
    return dir_list


local_path.touch()
dir_list = []
with open(local_path, "a+") as f:
    root_path = str(Path(local_path).parent.parent.parent)
    f.write(root_path + "\n")
    dir_list = get_all_dir(root_path, dir_list)
    print(dir_list)
    for path_single in dir_list:
        f.write(str(Path(path_single).absolute()) + "\n")
with open(local_path, "r") as f:
    print(f.readlines())
shutil.copyfile(local_path, destination_path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值